Template literals as string content in JSX

后端 未结 1 1952
旧巷少年郎
旧巷少年郎 2021-01-18 11:03

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() {
    c         


        
相关标签:
1条回答
  • 2021-01-18 11:18

    Template 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 recommend it because of debug issues as you would need to scan for brackets as the code expands

    <h1>{`Total count: ${this.state.totalCount}`}</h1>
    
    0 讨论(0)
提交回复
热议问题