How can I interpolate JSX with an expression in a string?

后端 未结 2 381
天命终不由人
天命终不由人 2020-12-28 11:40

Looking to achieve the following:

Obviously this compiles to:

相关标签:
2条回答
  • 2020-12-28 12:12

    You could use a react expression and then the javascript es6 string interpolation

    Something like this:

    <div someProp={`${SomeConstOrVariableOrExpression} simple text`}></div>
    
    0 讨论(0)
  • 2020-12-28 12:15

    Try this (ES5):

    <div className={'total total-' + obj.state}>...</div>
    

    Or with ES6+

    <div className={`total total-${obj.state}`}>...</div>
    

    Remember that everything between { and } is just Javascript, so the expression is 'total total-' + obj.state which is then set as the class of the DOM node.

    0 讨论(0)
提交回复
热议问题