I am creating a React application that requires using an external API to get some text. The problem is that the text is returned as a string with HTML tags to show emphasis, for example:
{ "text": ["The quick <em class='hl'>brown</em> fox"] }
I used dangerouslySetInnerHTML so
{this.props.text.map(function(snippet) { return <div dangerouslySetInnerHTML={{__html: snippet }}></div> })}
which worked most of the time, but I run instances in which the text returned by the API is formatted so that React throws the following error:
Uncaught (in promise) Error: Invariant Violation: Danger: Expected markup to render 7 nodes, but rendered 4
There seems to be no way to catch errors in the rendering function at the moment, so I can't just ignore the problematic ones and continue.
Is there a better way to handle things like text highlighting or highlighting with React, or is there perhaps a way to filter or catch lines that React might find problematic?
source share