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

What to do if the react tag does not wrap?

藏色散人
Release: 2022-12-29 10:44:39
Original
2241 people have browsed it

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进行处理,用
替换

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

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

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

推荐学习:《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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template