且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用字符串中的表达式插入 JSX?

更新时间:2023-11-07 19:04:46

试试这个 (ES5):

...

或者使用 ES6+

...

记住 {} 之间的一切都只是 Javascript,所以表达式是 'total total-' + obj.state然后设置为DOM节点的类.

Looking to achieve the following:

<div className="total total-{obj.state}">

Obviously this compiles to:

<div class="total total-{obj.state}">

Is there an elegant way in JSX to evaluate the expression in the {} within a string?

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.

上一篇 : :无法连接到“ipaddress"上的 MySQL 服务器 (110)下一篇 : ASP.NET MVC 4 - 如何加密连接字符串到我的生产SQL服务器?

相关阅读

推荐文章