3
3
[ ![ NPM] ( http://img.shields.io/npm/v/preact-render-to-string.svg )] ( https://www.npmjs.com/package/preact-render-to-string )
4
4
[ ![ Build status] ( https://github.com/preactjs/preact-render-to-string/actions/workflows/ci.yml/badge.svg )] ( https://github.com/preactjs/preact-render-to-string/actions/workflows/ci.yml )
5
5
6
- Render JSX and [ Preact] components to an HTML string.
6
+ Render JSX and [ Preact] ( https://github.com/preactjs/preact ) components to an HTML string.
7
7
8
8
Works in Node & the browser, making it useful for universal/isomorphic rendering.
9
9
10
10
\>\> ** [ Cute Fox-Related Demo] ( http://codepen.io/developit/pen/dYZqjE?editors=001 ) ** _ (@ CodePen)_ <<
11
11
12
-
13
12
---
14
13
15
-
16
14
### Render JSX/VDOM to HTML
17
15
18
16
``` js
19
- import render from ' preact-render-to-string' ;
17
+ import { render } from ' preact-render-to-string' ;
20
18
import { h } from ' preact' ;
21
19
/** @jsx h */
22
20
@@ -27,24 +25,23 @@ console.log(html);
27
25
// <div class="foo">content</div>
28
26
```
29
27
30
-
31
28
### Render Preact Components to HTML
32
29
33
30
``` js
34
- import render from ' preact-render-to-string' ;
31
+ import { render } from ' preact-render-to-string' ;
35
32
import { h , Component } from ' preact' ;
36
33
/** @jsx h */
37
34
38
35
// Classical components work
39
36
class Fox extends Component {
40
37
render ({ name }) {
41
- return < span class = " fox" > { name }< / span> ;
38
+ return < span class = " fox" > {name}< / span> ;
42
39
}
43
40
}
44
41
45
42
// ... and so do pure functional components:
46
43
const Box = ({ type, children }) => (
47
- < div class = {` box box-${ type} ` }> { children }< / div>
44
+ < div class = {` box box-${ type} ` }> {children}< / div>
48
45
);
49
46
50
47
let html = render (
@@ -57,22 +54,20 @@ console.log(html);
57
54
// <div class="box box-open"><span class="fox">Finn</span></div>
58
55
```
59
56
60
-
61
57
---
62
58
63
-
64
59
### Render JSX / Preact / Whatever via Express!
65
60
66
61
``` js
67
62
import express from ' express' ;
68
63
import { h } from ' preact' ;
69
- import render from ' preact-render-to-string' ;
64
+ import { render } from ' preact-render-to-string' ;
70
65
/** @jsx h */
71
66
72
67
// silly example component:
73
68
const Fox = ({ name }) => (
74
69
< div class = " fox" >
75
- < h5> { name }< / h5>
70
+ < h5> {name}< / h5>
76
71
< p> This page is all about {name}.< / p>
77
72
< / div>
78
73
);
@@ -89,14 +84,8 @@ app.get('/:fox', (req, res) => {
89
84
});
90
85
```
91
86
92
-
93
87
---
94
88
95
-
96
89
### License
97
90
98
- [ MIT]
99
-
100
-
101
- [ Preact ] : https://github.com/developit/preact
102
- [ MIT ] : http://choosealicense.com/licenses/mit/
91
+ [ MIT] ( http://choosealicense.com/licenses/mit/ )
0 commit comments