Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

📄 Universal rendering for Preact: render JSX and Preact components to HTML.

License

NotificationsYou must be signed in to change notification settings

preactjs/preact-render-to-string

 
 

Repository files navigation

NPMtravis-ci

Render JSX andPreact components to an HTML string.

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

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


Render JSX/VDOM to HTML

import{render}from'preact-render-to-string';import{h}from'preact';/**@jsx h */letvdom=<divclass="foo">content</div>;lethtml=render(vdom);console.log(html);// <div>content</div>

Render Preact Components to HTML

import{render}from'preact-render-to-string';import{h,Component}from'preact';/**@jsx h */// Classical components workclassFoxextendsComponent{render({ name}){return<spanclass="fox">{name}</span>;}}// ... and so do pure functional components:constBox=({ type, children})=>(<divclass={`box box-${type}`}>{children}</div>);lethtml=render(<Boxtype="open"><Foxname="Finn"/></Box>);console.log(html);// <div><span>Finn</span></div>

Render JSX / Preact / Whatever via Express!

importexpressfrom'express';import{h}from'preact';import{render}from'preact-render-to-string';/**@jsx h */// silly example component:constFox=({ name})=>(<divclass="fox"><h5>{name}</h5><p>This page is all about{name}.</p></div>);// basic HTTP server via express:constapp=express();app.listen(8080);// on each request, render and return a component:app.get('/:fox',(req,res)=>{lethtml=render(<Foxname={req.params.fox}/>);// send it back wrapped up as an HTML5 document:res.send(`<!DOCTYPE html><html><body>${html}</body></html>`);});

Migration guide

Migrating from 5.x to 6.x

The only breaking change introduced with the6.x is that thedefault exports have been removed in favor of named exports. To update, replace the default import in your code with a named one.

- import render from 'preact-render-to-string';+ import { render } from 'preact-render-to-string';

Similarily if you've been using thejsx renderer, the default import needs to be swapped with a named import:

- import render from 'preact-render-to-string/jsx';+ import { render } from 'preact-render-to-string/jsx';

Note: The named exports were already present in the5.x release line. So if you can't update today for any reason, you can apply the above changes safely to make a future update to6.x easier!


License

MIT

About

📄 Universal rendering for Preact: render JSX and Preact components to HTML.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp