Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including preact-render-to-string with all npm packages installed. Try it out:

require("preact/package.json"); // preact is a peer dependency. var preactRenderToString = require("preact-render-to-string")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

preact-render-to-string v3.7.0

Render JSX to an HTML string, with support for Preact components.


NPM travis-ci

Render JSX and Preact components to an HTML string.

Works in Node & the browser, making it useful for universal/isomorphic rendering.

>> Cute Fox-Related Demo (@ CodePen) <<


import render from 'preact-render-to-string';
import { h } from 'preact';
/** @jsx h */

let vdom = <div class="foo">content</div>;

let html = render(vdom);
// <div class="foo">content</div>

Render Preact Components to HTML

import render from 'preact-render-to-string';
import { h, Component } from 'preact';
/** @jsx h */

// Classical components work
class Fox extends Component {
    render({ name }) {
        return <span class="fox">{ name }</span>;

// ... and so do pure functional components:
const Box = ({ type, children }) => (
    <div class={`box box-${type}`}>{ children }</div>

let html = render(
    <Box type="open">
        <Fox name="Finn" />

// <div class="box box-open"><span class="fox">Finn</span></div>

Render JSX / Preact / Whatever via Express!

import express from 'express';
import { h } from 'preact';
import render from 'preact-render-to-string';
/** @jsx h */

// silly example component:
const Fox = ({ name }) => (
    <div class="fox">
        <h5>{ name }</h5>
        <p>This page is all about {name}.</p>

// basic HTTP server via express:
const app = express();

// on each request, render and return a component:
app.get('/:fox', (req, res) => {
    let html = render(<Fox name={} />);
    // send it back wrapped up as an HTML5 document:
    res.send(`<!DOCTYPE html><html><body>${html}</body></html>`);



RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free