Nono.MA

Render a React component as a string

APRIL 5, 2023

import * as React from 'react'
import * as Server from 'react-dom/server'

let Greet = () => <h1>Hello, Nono!</h1>
console.log(Server.renderToString(<div><Greet /></div>))
// <div><h1>Hello, Nono!</h1></div>

The tricky part is running this code.

You first need to build it, say, with esbuild, then execute it.

# Build with esbuild.
esbuild RenderToString.jsx --bundle --outfile=RenderToString.js

# Run with Node.js.
node RenderToString.js
# <div><h1>Hello, Nono!</h1></div>

BlogCodeReactEsbuildTil