7

How can I render formatted (unminified) HTML in React (SSR with Next.js)?

Expected output:

<div>
   <div>
       <input type="text" />
   </div>
</div>

Now receiving:

<div><div><input type="text"/></div></div>

TY!

2 Answers2

4

You need to format the output before actually returning to browser. So as for nextjs, first switch to the custom server nexjs custom server and routing and walkthrough this answer for pretty printing html. Using nextjs or react SSR, this may not be possible as a built-in configurable option.

Fawaz
  • 3,404
  • 3
  • 17
  • 22
2

You might be looking for dangerouslysetinnerhtml. It allows you to pass in a string that you want rendered as raw HTML.

function createMarkup() {
  return {__html: '<div><div><input type="text"/></div></div>'};
}

function MyComponent() {
  return <div dangerouslySetInnerHTML={createMarkup()} />;
}
Chase DeAnda
  • 15,963
  • 3
  • 30
  • 41