[英]How can I add line break as inner HTML systematically with ReactJS (+ Redux)?
I have these codes, in one of my component: <p>{ result }</p>
. 我在组件之一中有这些代码: <p>{ result }</p>
。 result
will be like 'asd\\n123'
, generated programmatically. result
将类似于以编程方式生成的'asd\\n123'
。
However the result in browser is "asd 123". 但是,浏览器中的结果是“ asd 123”。 It seems, the line break is not well received as inner HTML. 看来,换行符不能很好地作为内部HTML接收。 How can I have "asd" and then line break and then "123" displayed in my web browser? 如何在网络浏览器中显示“ asd”然后换行,然后显示“ 123”?
Try with: 尝试:
var result = 'asd' + (String.fromCharCode(13, 10) + 123;
<p>{ result }</p>
In ES6: 在ES6中:
var result = `asd${String.fromCharCode(13, 10)}123`;
<p>{ result }</p>
Or you have to replace your \\n: 或者,您必须替换\\ n:
result.split('\n').join(String.fromCharCode(13, 10));
<p>{ result }</p>
WORKING SNIPPET: 工作片段:
var result = "some\\nstring"; var res = result.split('\\n').join(String.fromCharCode(13, 10)); console.log(res);
Based on @casraf's comment, I modified the codes into like this: 基于@casraf的评论,我将代码修改为:
{ result != undefined ? result.split('\n').map(line => <p>{ line }</p>) : '' }
To solve this problem. 解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.