title: Wrap multiple strings in HTML the React way
body: <p>I'm building an entity highlighter so I can upload a text file, view the contents on the screen, then highlight words that are in an array. This is array is populated by the user when they manually highlight a selection e.g...</p>
  <p><code>const entities = ['John Smith', 'Apple', 'some other word'];</code></p>
  <p><code>This is my text document that is displayed on the screen. It contains a lot of text, and some of this text needs to be visually highlighted to the user once they manually highlight some text, like the name John Smith, Apple and some other word</code></p>
  <p>Now I want to visually highlight all instances of the entity in the text by wrapping it in some markup, and doing something like this works perfectly:</p>
  <pre><code>getFormattedText() {
      const paragraphs = this.props.text.split(/\n/);
      const { entities } = this.props;
  
      return paragraphs.map((p) =&gt; {
          let entityWrapped = p;
  
          entities.forEach((text) =&gt; {
          const re = new RegExp(`${text}`, 'g');
          entityWrapped =
              entityWrapped.replace(re, `&lt;em&gt;${text}&lt;/em&gt;`);
          });
  
          return `&lt;p&gt;${entityWrapped}&lt;/p&gt;`;
      }).toString().replace(/&lt;\/p&gt;,/g, '&lt;/p&gt;');
  }
  </code></pre>
  <p>...however(!), this just gives me a big string so I have to dangerously set the inner HTML, and therefor I can't then attach an onClick event 'the React way' on any of these highlighted entities, which is something I need to do.</p>
  <p>The React way of doing this would be to return an array that looks something like this:</p>
  <p><code>['This is my text document that is displayed on the screen. It contains a lot of text, and some of this text needs to be visually highlighted to the user, like the name', {}, {}, {}]</code> Where the <code>{}</code> are the React Objects containing the JSX stuff.</p>
  <p>I've had a stab at this with a few nested loops, but it's buggy as hell, difficult to read and as I'm incrementally adding more entities the performance takes a huge hit.</p>
  <p>So, my question is... what's the best way to solve this issue? Ensuring code is simple and readable, and that we don't get huge performance issues, as I'm potentially dealing with documents which are very long. Is this the time that I let go of my React morals and dangerouslySetInnerHTML, along with events bound directly to the DOM?</p>
  <h2>Update</h2>
  <p>@AndriciCezar's answer below does a perfect job of formatting the array of Strings and Objects ready for React to render, however it's not very performant once the array of entities is large (&gt;100) and the body of text is also large (&gt;100kb). We're looking at about 10x longer to render this as an array V's a string.</p>
  <p>Does anyone know a better way to do this that gives the speed of rendering a large string but the flexibility of being able to attach React events on the elements? Or is dangerouslySetInnerHTML the best solution in this scenario?</p>