Newline In React String [Solved] - The FreeCodeCamp Forum
Maybe your like
If you’ve ever wondered how to render the newline character (\n) in a string as an actual newline in your React app, read on.
Imagine you have the following boilerplate code:
import * as React from 'react'; import * as ReactDOM from 'react-dom'; ReactDOM.render( <div className="App"> </div>, document.getElementById('root') );You have a simple component and that you want to pass the following prop:
text: 'Line one\nLine two'And want the output to be:
Line one Line twoTurns out you have a couple of options: You could render each new line as a paragraph, or you could use the CSS white-space property.
Render each new line as a paragraphThis method involves splitting the text with the .split() method, then wrapping each new string in a paragraph element.
First, create a simple component:
import * as React from 'react'; import * as ReactDOM from 'react-dom'; function NewlineText(props) { } ReactDOM.render( <div className="App"> <NewlineText /> </div>, document.getElementById('root') );Next, pass the component the string 'Line one\nLine two\nLine three' as the prop text. Note that you’ll need to wrap the string in bracket ({}) so the newline characters are not interpreted as plain text:
import * as React from 'react'; import * as ReactDOM from 'react-dom'; function NewlineText(props) { } ReactDOM.render( <div className="App"> <NewlineText text={'Line one\nLine two\nLine three'} /> </div>, document.getElementById('root') );Next, declare a variable named text and set it equal to props.text:
function NewlineText(props) { const text = props.text; }Now you need to use the .split() method split the text string on each newline character and create an array of strings:
function NewlineText(props) { const text = props.text; const newText = text.split('\n'); }Then, use the .map() method to iterate through the array of strings and return each as a paragraph element:
function NewlineText(props) { const text = props.text; const newText = text.split('\n').map(str => <p>{str}</p>); }Finally, return newText from the component:
function NewlineText(props) { const text = props.text; const newText = text.split('\n').map(str => <p>{str}</p>); return newText; }And the original text will be rendered like so:
image-50752×189 2.3 KB
This works because paragraph elements are block level elements by default (display: block;). Browsers render each block level element on a new line.
However, this does also add some default upper and lower margins to each paragraph element. You can use CSS to remove the margins if you wish.
For bonus points, you could make your code a bit more succinct. For example:
import * as React from 'react'; import * as ReactDOM from 'react-dom'; function NewlineText(props) { const text = props.text; return text.split('\n').map(str => <p>{str}</p>); } ReactDOM.render( <div className="App"> <NewlineText text={'Line one\nLine two\nLine three'} /> </div>, document.getElementById('root') ); Use the CSS white-space propertyThis method involves using the white-space property to preserve the newline characters in the original string.
First, create a simple component and pass it 'Line one\nLine two\nLine three' as the prop text:
import * as React from 'react'; import * as ReactDOM from 'react-dom'; function NewlineText(props) { } ReactDOM.render( <div className="App"> <NewlineText text={'Line one\nLine two\nLine three'} /> </div>, document.getElementById('root') );Next, declare a variable named text and set it equal to props.text:
function NewlineText(props) { const text = props.text; }Then, just wrap text in a div element and return it:
function NewlineText(props) { const text = props.text; return <div>{text}</div>; }Finally in your CSS file, target the div element and set the white-space property to pre-wrap:
div { white-space: pre-wrap; }The value pre-wrap preserves all white space characters, including line breaks with newline characters and <br>.
Now your code should look like this:
import * as React from 'react'; import * as ReactDOM from 'react-dom'; function NewlineText(props) { const text = props.text; return <div>{text}</div>; } ReactDOM.render( <div className="App"> <NewlineText text={'Line one\nLine two\nLine three'} /> </div>, document.getElementById('root') );Which results in the following output:
image-51747×128 1.64 KB
These are just two ways to render newlines in React strings. Here’s a Repl.it snippet with both methods side-by-side:
Now go forth and render all the new lines.
Tag » How To Line Break In React
-
How Can I Insert A Line Break Into A
Component In React ... -
React.js Br Tag And AJAX Request - Pluralsight
-
Make Line Breaks Work When You Render Text In A React Or Vue ...
-
How To Insert A Line Break Into A Text Component In React Native?
-
How To Create A New Line In JSX And Reactjs | The JavaScript Diaries
-
How To Add Line Break In React Js Code Example
-
React Native How To Add Line Break To Text Component? - Cloudhadoop
-
How To Insert Line Break In React Native | Voters
-
How To Insert A Line Break Into A Text Component In ... - Source Freeze
-
How Can I Insert A Line Break Into A Text Component In React Native
-
Line Break In React With Code Examples
-
React Newline To Break (nl2br) - Kevin Simper - Medium
-
Prevent Line Breaks With CSS. - Medium
-
How Can I Insert A Line Break Into A ... - Top Questions And Answers