React-i18next: break lines in JSON String

Created on 25 Aug 2017  路  5Comments  路  Source: i18next/react-i18next

I'm struggling to break lines within the string in my JSON language file.

This is what I already tried, which doesn't break a new line:

line: "This is a line. \n This is another line. \n Yet another line",

line: ("This is a line."+ <br/> + "This is another line. \n Yet another line"),

line: ('This is a line. <br/> This is another line. \n Yet another line'),

I obviously try to make a new line after each sentence. This is how I call it:

<TooltipLink onClick={() => {
    this.toggleHelpTextDialog(t('test:test.line'));
}}/>

Any ideas? Thanks!

Most helpful comment

You can't do that in react - not without doing some dangerously insert html: https://zhenyong.github.io/react/tips/dangerously-set-inner-html.html

but what you can do:

...
render() {
  return (
    <div>
       {
         t('test:test.line').split('/n').map(line => <p>{line}</p>)
       }
    </div>
  )
}
...

All 5 comments

You can't do that in react - not without doing some dangerously insert html: https://zhenyong.github.io/react/tips/dangerously-set-inner-html.html

but what you can do:

...
render() {
  return (
    <div>
       {
         t('test:test.line').split('/n').map(line => <p>{line}</p>)
       }
    </div>
  )
}
...

alternative to above solution -> use markdown

https://github.com/acdlite/react-remarkable

closing this for now...hope one of the provided options helped to solve the problem

https://medium.com/@kevinsimper/react-newline-to-break-nl2br-a1c240ba746 article on break line in react

Adding white-space: pre-line via CSS did the trick for me :)

Was this page helpful?
0 / 5 - 0 ratings