Search code examples
javascriptreactjsjsxrenderingnewline

what is the difference between ReactJS convert \r\n in this two way and why?


screenshot of the issue

Can someone tell me why, thank you

Is it because one is a text node and another is a variable


Solution

  • Those special characters don't work in HTML, the literal text '\r\n' will be rendered in the browser.

    Using the backtick ` character creates a Template String, which has a bit more 'syntactic sugar' and functionality than raw HTML text. It is a Javascript string and you can use JS special characters (e.g. \r\n) inside it.

    HTML uses character encodings instead, which look like this: 
. This represents the \n 'newline' character, and will work in your example.