Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a new line in a JSX string inside a paragraph - React

I have the following React code:

render() {
  const str = 'Line 1. **new-line** Line 2.';
  return (
    <p> {str} </p>
  );
}

I would like the output to be:

Line 1.
Line 2.

That is - to add a new line between the words. I have tried \n but it does not work.

How could this be accomplished?

Edit: The string is received from a server.

like image 588
Poogy Avatar asked May 08 '18 08:05

Poogy


People also ask

How add new line in JSX React?

You can use \n where you want to break the line.

How do you write JSX inside JSX?

Embedding Expressions in JSX You can put any valid JavaScript expression inside the curly braces in JSX. For example, 2 + 2 , user. firstName , or formatName(user) are all valid JavaScript expressions. In the example below, we embed the result of calling a JavaScript function, formatName(user) , into an <h1> element.


1 Answers

Set CSS-Style for the paragraph as below, it will break line on \n and wrap text according to parent width.

white-space: pre-wrap;

or

white-space: pre-line;
like image 179
Eltaf Hussain Avatar answered Sep 22 '22 17:09

Eltaf Hussain