Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React text prop linebreak

So I have a prop called data which takes a string as an argument, for example

<MyComp 
  data="Some text, some other text"
/>

How can I add a line break after Some text, let's say? would a simple escaped /n tag work( I mean, is it best practice? )

like image 550
J.Doo Avatar asked Apr 22 '26 02:04

J.Doo


2 Answers

Just use curly braces when passing props and add \n:

    <MyComp 
      data={"Some text,\nsome other text"}
    />

and add css property to element white-space: pre-wrap;

like image 167
Bluorenge Avatar answered Apr 23 '26 18:04

Bluorenge


It totally depends on how you are going to use your data prop. Escaped Characters (\n) will not work in JSX. In this case, I would suggest splitting the lines over multiple props or passing an Array with multiple lines.

However, if you are going to use it for an input element like a textarea, you can use Escaped Characters just fine.

like image 22
Chris Avatar answered Apr 23 '26 18:04

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!