" tag; 2. In JSX, use "dangerousSetInnerHTML" to implement line wrapping and set the css attribute "white-space:pre-wrap" is enough."/> " tag; 2. In JSX, use "dangerousSetInnerHTML" to implement line wrapping and set the css attribute "white-space:pre-wrap" is enough.">

Home  >  Article  >  Web Front-end  >  What to do if the react tag does not wrap?

What to do if the react tag does not wrap?

藏色散人
藏色散人Original
2022-12-29 10:44:392175browse

react标签不换行的解决办法:1、对返回的数据中的“\n”进行处理,用“
”标签替换;2、在JSX中,配合“dangerousSetInnerHTML”实现换行,并设置css属性“white-space:pre-wrap”即可。

What to do if the react tag does not wrap?

本教程操作环境:Windows10系统、react18.0.0版、Dell G3电脑。

react标签不换行怎么办?

React项目/JSX中‘\n’不换行问题解决

最近在React项目中,后端返回的字符串中用\n提供给前端进行换行,但是在前端页面并没用实现换行,我在网上查了很多资料,找到很多方法,但是都没有实现换行,最后,我自己无意间通过组合试出来了。

1 首先,对返回的数据中的\n进行处理,用076402276aae5dbec7f672f8f4e5cc81替换

eg:const info = 'React\n Vue\n Angular';
const replaceInfo = info.replace(/\\n/g, &#39;<br/>);

2 在JSX中,配合dangerousSetInnerHTML实现换行,设置css属性:white-space:pre-wrap

eg:<div style={{whiteSpace: &#39;pre-wrap&#39;}} dangerousSetInnerHTML={{__html: replaceInfo}}>

推荐学习:《react视频教程

The above is the detailed content of What to do if the react tag does not wrap?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn