How do you render content from a CMS with React?

Home / Uncategorized / How do you render content from a CMS with React?

Question:
My react app gets content from WordPress. The editors can add content to WP’s editor where they can style the text and format it.

How do I render this text on the client site with react?

So far I have tried using dangerouslySetInnerHTML, but the name suggests to me that I shouldn’t be using it – at least not very often, but all my content comes from WordPress.

So is there another way of rendering HTML strings in react?


Answer:
You can use dangerouslySetInnerHTML, it is Reacts replacement for using innerHTML in the browser DOM. In general, setting HTML from code can be a bit risky because you can inadvertently expose your users to XSS attacks (cross-site scripting).

You can set HTML directly from React, but you type the dangerous* in the name (and pass an object with a __html) to remind yourself that it’s dangerous.

In your case, since the content comes from WP where authorized editors edit the content, it should be fine.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *