React Newline to break (nl2br)

Because you know that everything in React is functions, you can’t really do this

Since that would return a string with DOM nodes inside, that is not allowed either, because has to be only a string.

You then can try do something like this:

That is not allowed either because again React is pure functions and two functions can be next to each other.

tldr. Solution

Now we’re wrapping each line-break in a span, and that works fine because span’s has display inline. Now we got a working nl2br line-break solution.

And ES6 version

And with React Fragments

From: https://medium.com/@kevinsimper/react-newline-to-break-nl2br-a1c240ba746

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注