I have text inside a variable that I need to separate in paragraphs, and at the same time respect the manual breaklines.

This is my code:

    {description.split('\n\n').map(paragraph => (
                (line, index) =>
                    index > 0 ? (
                            <br />
                    ) : (

I am not specially happy with the code. It is not very readable. Any ideas how to improve it?


2 Answers 2


Two things that help in cases like this (IMHO):

  • Extract a function for more complex logic or a Array.map callback (e.g. to give a descriptive name and/or avoid nesting)
  • Provide descriptive names for non-obvious stuff (e.g. I'd prefer firstLine and remainingLines (or something similar) over using index)

I'd probably do something like this:


// If you are in a class, this could be a method on the class
function renderParagraph(paragraph) {
  // With fancy ES6:
  const [firstLine, ...rest] = paragraph.split('\n')
  // Or ES5:
  const lines = paragraph.split('\n')
  const firstLine = lines[0]
  const rest = lines.slice(1)

  return (
      {rest.map(line => (
        // React.Fragment doesn’t create a wrapper element in the DOM.
        // If you don’t care about that, you can use div instead
          <br />
  • \$\begingroup\$ This looks very readable. Asking because I don't know, why did you use React.Fragment? Is it better than span? \$\endgroup\$
    – JaDogg
    Commented Feb 14, 2019 at 13:24
  • 1
    \$\begingroup\$ OK I read the documentation reactjs.org/docs/fragments.html, and fragments are clearly better. Because it avoids extra wrapper elements. Could you please add this part to the answer as well. ;) \$\endgroup\$
    – JaDogg
    Commented Feb 14, 2019 at 13:27
  • 1
    \$\begingroup\$ Good suggestion, thanks. I changed the code comment to clarify my preference of React.Fragment \$\endgroup\$
    – inyono
    Commented Feb 14, 2019 at 19:46

You almost got it perfectly.

The only bit that could be changed is the part putting <br/> tags between lines. If you want to build a JSX element by putting nodes between each array string, I would recommend using reduce.

Reduce will iterate over your array starting by the indexes 0 and 1. By outputting an array like the following : [total, <br />, line] you can build up your paragraph :

paragraph.split('\n').reduce((total, line) => [total, <br />, line])

Working example :

const text = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna 

Ut enim ad minim veniam, quis nostrud exercitation 
ullamco laboris nisi ut aliquip ex ea commodo consequat.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum 
dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat non proident, 
sunt in culpa qui officia deserunt mollit anim id est laborum`

const Renderer = () =>
        {text.split('\n\n').map(paragraph =>
                {paragraph.split('\n').reduce((total, line) => [total, <br />, line])}

	<Renderer />,
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id='root'/>


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.