Skip to content

Instantly share code, notes, and snippets.

@pgmoir
Created February 26, 2020 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgmoir/cd785992b9825118f01f13f4d38bfeda to your computer and use it in GitHub Desktop.
Save pgmoir/cd785992b9825118f01f13f4d38bfeda to your computer and use it in GitHub Desktop.
ReactJS - convert line breaks
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