Render curly braces as plain text in React/JSX

前端 未结 3 409
再見小時候
再見小時候 2020-12-06 16:13

I am having problems displaying { and } as text in React. I saw a similar question that someone said to wrap the entire string in curlies, but this is not working:



        
相关标签:
3条回答
  • 2020-12-06 16:34

    If you'd like to render curly braces as plain text within a JSX document simply use the HTML character codes.

    Left Curly Brace { : {

    Right Curly Brace } : }

    0 讨论(0)
  • 2020-12-06 16:37

    I think the issue is just a typo. You have this:

    return (<p>{"{{}}"}<p>)
    

    but you need this (note the closing p tag instead of another opening one):

    return (<p>{"{{}}"}</p>)
    
    0 讨论(0)
  • 2020-12-06 16:37

    You can even use string interpolation in ES6. Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. Template literals are enclosed by the backtick () (grave accent) character instead of double or single quotes.

    eg: return (<p>{`{{}}`}<p>)
    
    0 讨论(0)
提交回复
热议问题