Use or similar tags in ReactJS using JSX

后端 未结 2 1551
Happy的楠姐
Happy的楠姐 2021-01-02 18:00

I am trying to use ReactJS with JSX to create a proof of concept for a styleguide.

I wanted to be able to display the raw html of how to call on components doing t

2条回答
  •  执笔经年
    2021-01-02 18:29

    In your example, react interprets your code as JSX. You need to use JSX-save coding. The easiest is to include a string literal:

    {`
      
    `}

    Explanation: Parsing your input starts at

     in JSX mode.  The opening bracket { switches the parser into JavaScript mode to include computed values into the output. Now the backquote ` switches the parser into string-parsing mode. This can span multiple lines.  Inside string parsing mode, JSX-special characters <>{} are not special anymore.  But you must escape the two characters that are special inside multi-line strings: The back quote ` and the dollar sign $.

    After parsing your coding, the final back quote switches back to JavaScript mode, the following } switches back to JSX mode and

    completes your element.

    React will automatically convert your string into safe HTML.

提交回复
热议问题