[英]Reactjs - how can i make line break in template strings and return for rendering?
You need to display the following content formatted on the screen:您需要在屏幕上显示以下格式化的内容:
divs:
<div>my text</div> <div>my text</div>
i tried to do this, but everything came out without line break:我试图这样做,但一切都没有换行:
const HtmlCreator=()=>{
let text="my text";
return (`divs: \n <div>${text}</div> <div>${text}</div> `)
{
return (
<div>
<p><HtmlCreator/></p>
</div>
)
You can use <br />
, but you'll need a wrapper.您可以使用
<br />
,但您需要一个包装器。 For that, you can use <React.Fragment>
:为此,您可以使用
<React.Fragment>
:
import React, { Fragment } from "react";
const HtmlCreator = () => {
let text = "my text";
return (
<Fragment>
divs
<br />
{`<div>${text}</div> <div>${text}</div>`}
</Fragment>
);
};
Your question implies you'd like to show the HTML itself (escape it) and not render it.您的问题意味着您想显示 HTML 本身(转义它)而不是呈现它。 If you meant the latter, you could do:
如果你的意思是后者,你可以这样做:
const HtmlCreator = () => {
let text = "my text";
return (
<Fragment>
divs
<br />
<div>{text}</div>
<div>{text}</div>
</Fragment>
);
};
Add whiteSpace:'pre'
to your style :将
whiteSpace:'pre'
添加到您的样式中:
<p style={{whiteSpace:'pre'}}><HtmlCreator /></p>
this is useful when you cannot modify the original data.当您无法修改原始数据时,这很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.