Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork97
📄 Universal rendering for Preact: render JSX and Preact components to HTML.
License
preactjs/preact-render-to-string
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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) <<
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>
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>
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>`);});
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!
About
📄 Universal rendering for Preact: render JSX and Preact components to HTML.
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.