ReactJS将这两种方式转换的区别是什么,为什么?

huangapple go评论81阅读模式
英文:

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

问题

为什么,谢谢

是因为一个是文本节点,另一个是变量。

英文:

ReactJS将这两种方式转换的区别是什么,为什么?

Can someone tell me why, thank you

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

答案1

得分: 2

<div>123
123</div>
<div>{'123\r\n123'}</div>
英文:
&lt;div&gt;123\r\n123&lt;/div&gt;

The former one is pure HTML, i.e. what is between the opening and closing tags will be displayed as is.

&lt;div&gt;{&#39;123\r\n123&#39;}&lt;/div&gt;

Whereas the latter one is JSX, where the inner content is a string (programmatically speaking) - notice the use of <kbd>{</kbd><kbd>}</kbd> in the tags - thus \r and \n being escape characters

答案2

得分: 1

那些特殊字符在HTML中不起作用,字面文本'\r\n'将在浏览器中呈现。

使用反引号 ` 字符创建一个模板字符串,它比原始HTML文本具有更多的'syntactic sugar'和功能。它是一个JavaScript字符串,你可以在其中使用JS特殊字符(例如\r\n)。

HTML使用字符编码,看起来像这样:&amp;#13;。这代表了\n 'newline'字符,并将在你的示例中起作用。

英文:

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: &amp;#13;. This represents the \n 'newline' character, and will work in your example.

huangapple
  • 本文由 发表于 2023年2月23日 21:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定