Skip to content

Commit 8fa1d42

Browse files
theKasheygregberge
authored andcommitted
fix: fix various bugs (#857)
* safe define displayName, fix #845 * stand children did not exists, fix #843 * improve TS documentation * add SSR+HRM example * subrender - fix deferred updates * dont fix package version * should have react as a peer dep * use didMount instead of will, related to #860
1 parent 99da77b commit 8fa1d42

25 files changed

+5242
-235
lines changed

Diff for: README.md

+13
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ Just add babel-loader into your webpack configuration, with RHL-only config.
127127
}
128128
```
129129

130+
You **also have to modify tsconfig**
131+
132+
```json
133+
{
134+
...
135+
"module": "commonjs", // module should be commonjs, as long "imports" in TS and Babel works differently.
136+
"target": "es6", // target should be es6, or RHL will be unable to change some class members
137+
...
138+
}
139+
```
140+
141+
Yet again - module = es6 **will not work**.
142+
130143
### Parcel Bundler
131144

132145
Parcel's HRM is a bit different.

Diff for: examples/SSR/.babelrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
"env",
4+
"react"
5+
],
6+
"plugins": [
7+
"react-hot-loader/babel",
8+
"transform-class-properties",
9+
"dynamic-import-node"
10+
]
11+
}

Diff for: examples/SSR/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Diff for: examples/SSR/package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "react-hot-loader-ssr",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"scripts": {
6+
"build": "webpack",
7+
"start": "babel-node ./src/server.js",
8+
"start:webpack": "webpack-dev-server --hot"
9+
},
10+
"devDependencies": {
11+
"babel-core": "^6.26.0",
12+
"babel-loader": "^7.1.2",
13+
"babel-plugin-transform-class-properties": "^6.24.1",
14+
"babel-preset-env": "^1.6.1",
15+
"babel-preset-react": "^6.24.1",
16+
"html-webpack-plugin": "^2.30.1",
17+
"webpack": "^3.10.0",
18+
"webpack-dev-server": "^2.9.7"
19+
},
20+
"dependencies": {
21+
"babel-cli": "^6.26.0",
22+
"express": "^4.16.2",
23+
"react": "^16.2.0",
24+
"react-dom": "^16.2.0",
25+
"react-hot-loader": "next",
26+
"react-portal": "^4.1.2"
27+
}
28+
}

Diff for: examples/SSR/src/App.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { hot, setConfig } from 'react-hot-loader'
2+
import * as React from 'react'
3+
import Counter from './Counter'
4+
// import hidden from './HiddenComponent';
5+
6+
const App = () => (
7+
<h1>
8+
<p>{40}!</p>
9+
<Counter />
10+
<div>
11+
{/*{hidden().counter}*/}
12+
SSR, and I work fine!
13+
</div>
14+
</h1>
15+
)
16+
17+
setConfig({ logLevel: 'debug' })
18+
19+
export default hot(module)(App)

Diff for: examples/SSR/src/Counter.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react'
2+
3+
const RAND = 1 //Math.round(Math.random() * 1000)
4+
5+
class Counter extends React.Component {
6+
state = { count: 0 }
7+
gen = 0
8+
9+
componentDidMount() {
10+
this.setState({
11+
count: RAND,
12+
})
13+
}
14+
15+
componentWillUnmount() {
16+
clearInterval(this.interval)
17+
}
18+
19+
render() {
20+
// gen should change. count - no.
21+
return (
22+
<span>
23+
{this.state.count}:{this.gen++}
24+
</span>
25+
)
26+
}
27+
}
28+
29+
export default Counter

Diff for: examples/SSR/src/HiddenComponent.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react'
2+
import Counter from './Counter'
3+
4+
const hidden = function() {
5+
return {
6+
counter: () => (
7+
<div>
8+
this is hidden counter(<Counter />)
9+
</div>
10+
),
11+
}
12+
}
13+
14+
export default hidden

Diff for: examples/SSR/src/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'babel-polyfill'
2+
import React from 'react'
3+
import { hydrate } from 'react-dom'
4+
import App from './App'
5+
6+
//const root = document.createElement('div')
7+
//document.body.appendChild(root)
8+
9+
const root = document.getElementById('root');
10+
hydrate(<App />, root)

Diff for: examples/SSR/src/server.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import express from 'express'
2+
import React from 'react'
3+
import { renderToString } from 'react-dom/server'
4+
import App from './App'
5+
import template from './template'
6+
7+
const server = express()
8+
9+
server.use('/dist', express.static('dist'))
10+
11+
server.get('/', (req, res) => {
12+
const app = <App />
13+
14+
const appString = renderToString(app)
15+
16+
res.send(
17+
template({
18+
body: appString,
19+
}),
20+
)
21+
})
22+
23+
server.listen(8080)
24+
25+
console.log('server ready')

Diff for: examples/SSR/src/template.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default ({ body }) => {
2+
return `
3+
<!DOCTYPE html>
4+
<html>
5+
<head>
6+
</head>
7+
8+
<body>
9+
<div id="root">${body}</div>
10+
</body>
11+
12+
<script src="/dist/bundle.js"></script>
13+
</html>
14+
`
15+
}

Diff for: examples/SSR/webpack.config.babel.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-disable */
2+
const path = require('path')
3+
const webpack = require('webpack')
4+
const HtmlWebpackPlugin = require('html-webpack-plugin')
5+
6+
module.exports = {
7+
entry: ['webpack/hot/dev-server', './src/index'],
8+
output: {
9+
path: path.join(__dirname, 'dist'),
10+
filename: 'bundle.js',
11+
},
12+
module: {
13+
rules: [
14+
{
15+
//exclude: /node_modules|packages/, // should work without exclude
16+
test: /\.js$/,
17+
use: 'babel-loader',
18+
},
19+
],
20+
},
21+
plugins: [
22+
new HtmlWebpackPlugin(),
23+
new webpack.NamedModulesPlugin(),
24+
new webpack.HotModuleReplacementPlugin(),
25+
],
26+
}

0 commit comments

Comments
 (0)