0

Is it possible to have line breaks within one value of an JSON Object? I tried with \n with br-tag and with template literals like bla bla ${\n} bla bla bla, but it's not working.

My code looks like this in the Object

export const Data = [
{
    id: 1,
    item: 'blablabla',
    detail:
        'bla bla bla. <here I want a line break> bla bla bla'
}]

Thanks for your help

2
  • If you use the prop dangerouslySetInnerHTML={{ __html: 'text <br/> text' }} on the element where you want to render the text you can do it with <br/> Commented Feb 26, 2021 at 14:45
  • Thanks Nicolae. I read about dangerouslySetInnerHTML and I'm not sure, if I should use it. Is there any other possibility to have a line break in an external data object value? Commented Feb 26, 2021 at 15:00

1 Answer 1

1

The best approach I found is to use the css property white-space: "pre-wrap"; , it will let you use line breaks without <br/>

I would also recommend to write the text with template literals so you don't need to use \n

export const Data = [
    {
        id: 1,
        item: 'blablabla',
        detail: "bla bla bla. \n bla bla bla",
        betterDetail: 
            `it will
             also
             works like this
             `
     }]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.