简体   繁体   English

如何在 React JS 中的 html 属性中编写 JavaScript 代码

[英]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.我知道上面的属性loadingMessage值在语法上是错误的。 But my need is, I need to get that programName from state and append to loadingMessage attribute value.但我的需要是,我需要从 state 中获取那个programName加到loadingMessage属性值。 How can I do this?我该怎么做? Any help will be appreciated.任何帮助将不胜感激。

You can write javascript inside the curly brackets, just concatenate the string in there.您可以在大括号内编写 javascript,只需将其中的字符串连接起来即可。

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

or use a template literal :或使用模板文字

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM