admin管理员组文章数量:1295130
I'm wondering whats the best practice for strings values mixed with variables inside JSX tags, I've listed the options I'm familiar with:
render() {
const {totalCount} = this.state;
const totalCountStr = `Total count: ${totalCount}`;
return (
<div>
<h1>Total count: {totalCount}</h1> // 1
<h1>`Total count: ${totalCount}`</h1> // 2
<h1>{totalCountStr}</h1> // 3
</div>
);
}
What's the best practice or the use cases to use them differently?
Thanks!
I'm wondering whats the best practice for strings values mixed with variables inside JSX tags, I've listed the options I'm familiar with:
render() {
const {totalCount} = this.state;
const totalCountStr = `Total count: ${totalCount}`;
return (
<div>
<h1>Total count: {totalCount}</h1> // 1
<h1>`Total count: ${totalCount}`</h1> // 2
<h1>{totalCountStr}</h1> // 3
</div>
);
}
What's the best practice or the use cases to use them differently?
Thanks!
Share Improve this question asked Sep 4, 2018 at 15:01 TimTim 1673 silver badges9 bronze badges 1- I don't think #2 works. You could inline #3, but it would be very unappealing, especially pared to #1. – Bergi Commented Sep 4, 2018 at 15:09
1 Answer
Reset to default 9Template literals aren't supported by React JSX currently. The correct way to do it is like so:
<h1>Total count: {this.state.totalCount}</h1>
Edit: Your third way is also correct, but I personally wouldn't remend it because of debug issues as you would need to scan for brackets as the code expands
<h1>{`Total count: ${this.state.totalCount}`}</h1>
本文标签: javascriptTemplate literals as string content in JSXStack Overflow
版权声明:本文标题:javascript - Template literals as string content in JSX - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741610504a2388230.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论