Skip to content

Commit b79bbec

Browse files
Henritimneutkens
Henri
authored andcommittedSep 12, 2018
Add react-jss example (#5140)
Added a clear example on how to use react-jss with injecting the styles on the server. cssinjs/jss#457
1 parent c2a7766 commit b79bbec

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed
 

‎examples/with-react-jss/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["next/babel"]
3+
}

‎examples/with-react-jss/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-redux)
2+
3+
# react-jss example
4+
5+
## How to use
6+
7+
### Using `create-next-app`
8+
9+
Execute [`create-next-app`](https://github.com/segmentio/create-next-app) with [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) or [npx](https://github.com/zkat/npx#readme) to bootstrap the example:
10+
11+
```bash
12+
npx create-next-app --example with-react-jss with-react-jss-app
13+
# or
14+
yarn create next-app --example with-react-jss with-react-jss-app
15+
```
16+
17+
### Download manually
18+
19+
Download the example:
20+
21+
```bash
22+
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-react-jss
23+
cd with-react-jss
24+
```
25+
26+
Install it and run:
27+
28+
```bash
29+
npm install
30+
npm run dev
31+
# or
32+
yarn
33+
yarn dev
34+
```
35+
36+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
37+
38+
```bash
39+
now
40+
```
41+
42+
## The idea behind the example
43+
44+
This example shows how to integrate react-jss and jss in Next.js
45+
46+
The critical styles will be injected into the head when server rendered.

‎examples/with-react-jss/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "with-react-jss",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"dev": "next",
6+
"build": "next build",
7+
"start": "next start"
8+
},
9+
"dependencies": {
10+
"next": "latest",
11+
"react": "^16.0.0",
12+
"react-dom": "^16.0.0",
13+
"react-jss": "8.6.1"
14+
},
15+
"license": "ISC"
16+
}

‎examples/with-react-jss/pages/_app.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import App from 'next/app'
2+
3+
export default class MyApp extends App {
4+
componentDidMount () {
5+
const style = document.getElementById('server-side-styles')
6+
7+
if (style) {
8+
style.parentNode.removeChild(style)
9+
}
10+
}
11+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react'
2+
import Document, { Head, Main, NextScript } from 'next/document'
3+
import {
4+
SheetsRegistry,
5+
JssProvider
6+
} from 'react-jss'
7+
8+
export default class JssDocument extends Document {
9+
static getInitialProps (ctx) {
10+
const registry = new SheetsRegistry()
11+
const page = ctx.renderPage(App => props => (
12+
<JssProvider registry={registry}>
13+
<App {...props} />
14+
</JssProvider>
15+
))
16+
17+
return {
18+
...page,
19+
registry
20+
}
21+
}
22+
23+
render () {
24+
return (
25+
<html>
26+
<Head>
27+
<style id='server-side-styles'>
28+
{this.props.registry.toString()}
29+
</style>
30+
</Head>
31+
32+
<body>
33+
<Main />
34+
<NextScript />
35+
</body>
36+
</html>
37+
)
38+
}
39+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
import injectSheet from 'react-jss'
3+
4+
const styles = {
5+
container: {
6+
marginTop: 100,
7+
textAlign: 'center'
8+
},
9+
10+
header: {
11+
fontSize: 24,
12+
lineHeight: 1.25
13+
}
14+
}
15+
16+
function Index (props) {
17+
return (
18+
<div className={props.classes.container}>
19+
<h1 className={props.classes.header}>
20+
Example on how to use react-jss with Next.js
21+
</h1>
22+
</div>
23+
)
24+
}
25+
26+
export default injectSheet(styles)(Index)

0 commit comments

Comments
 (0)
Please sign in to comment.