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 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That was what I needed!
@NikolaKnežević glad it helped!
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()} />;
}

1 Comment

Sorry, but that's not my question. I mean entire page is rendered minified (in 1 line).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.