Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write JavaScript code inside html attributes in React JS

return(
  <div>
    <Loading loadingMessage="Running "{this.state.programName}" program"/>
  </div>
);

I know that above attribute loadingMessage value is syntactically wrong. But my need is, I need to get that programName from state and append to loadingMessage attribute value. How can I do this? Any help will be appreciated.

like image 617
Chetan Motamarri Avatar asked Oct 16 '25 03:10

Chetan Motamarri


1 Answers

You can write javascript inside the curly brackets, just concatenate the string in there.

return (
    <div>
        <Loading
            loadingMessage={"Running " + this.state.programName + " program"}
        />
    </div>
);

or use a template literal:

<Loading loadingMessage={`Running ${this.state.programName} program`}/>

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!