Created
February 26, 2020 10:47
-
-
Save pgmoir/cd785992b9825118f01f13f4d38bfeda to your computer and use it in GitHub Desktop.
ReactJS - convert line breaks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addLineBreaksToParagraph = string => | |
string.split('\n').map((text, index) => ( | |
<p key={index}> | |
{text} | |
</p> | |
)); | |
// e.g. | |
<div>{addLineBreaksToParagraph(heading)}</div> | |
const addLineBreaks = string => | |
string.split('\n').map((text, index) => ( | |
<React.Fragment key={index}> | |
{text} | |
<br /> | |
</React.Fragment> | |
)); | |
// e.g. | |
<h1>{addLineBreaks(heading)}</h1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment