Skip to content

feat: React16.6 compatibility #1084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,48 @@ import { setConfig, cold } from 'react-hot-loader'
setConfig({
onComponentRegister: (type, name, file) =>
file.indexOf('node_modules') > 0 && cold(type),

// some components are not visible as top level variables,
// thus its not known where they were created
onComponentCreate: (type, name) => file.indexOf('styled') > 0 && cold(type),
})
```

! To be able to "cold" components from 'node_modules' you have to apply babel to node_modules, while this
folder is usually excluded.
You may add one more babel-loader, with only one React-Hot-Loader plugin inside to solve this.
**Consider using webpack-loader** for this.

##### React-Hooks

React-hot-loader does not support React 16.7 Hooks at all.
You have to

* _cold_ components using hooks.

```js
import { setConfig, cold } from 'react-hot-loader'
setConfig({
onComponentCreate: (type, name) =>
(String(type).indexOf('useState') > 0 ||
String(type).indexOf('useEffect') > 0) &&
cold(type),
})
```

* _set a special flag_

```js
import { setConfig, cold } from 'react-hot-loader'
setConfig({
onComponentCreate: (type, name) =>
(String(type).indexOf('useState') > 0 ||
String(type).indexOf('useEffect') > 0) &&
cold(type),
})
```

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code snippet and the one above are identical. Is this expected or that was supposed to be something else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💩 this is something unfinished. Idea was to provide yet-another-api, to specify special flags, component should be created with.
The current approach to "fix" hooks with setConfig({pureSFC: true}) is breaking Relay-modern.

I also could not put this logic inside, as long I could not cast every component to a string - emotion throws an Error and should be fixed first.


PS: `react-emotion` would break due this operation.

## API

Expand Down
3 changes: 1 addition & 2 deletions examples/all-possible-containers/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const exclude = absPath => /node_modules/.test(absPath)
const mode = 'production'
//process.env.NODE_ENV || 'development'
const mode = process.env.NODE_ENV || 'development'

const production = mode === 'production'

Expand Down
2 changes: 1 addition & 1 deletion examples/styled-components/.babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ["env", "react"],
"plugins": ["react-hot-loader/babel", "transform-class-properties"]
"plugins": ["react-hot-loader/babel", "transform-class-properties", "dynamic-import-node"]
}
15 changes: 8 additions & 7 deletions examples/styled-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"html-webpack-plugin": "^2.30.1",
"webpack": "^3.10.0",
"webpack-dev-server": "^2.9.7"
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10"
},
"dependencies": {
"emotion": "^8.0.12",
"react": "^16.3.2",
"react-dom": "^16.2.0",
"react-emotion": "^8.0.12",
"react": "^16.6.1",
"react-dom": "^16.6.1",
"react-emotion": "^9.2.12",
"react-hot-loader": "^4.1.1",
"styled-components": "^2.4.0"
"styled-components": "^4.0.3"
}
}
25 changes: 22 additions & 3 deletions examples/styled-components/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import emoStyled from 'react-emotion'
import Counter from './Counter'

const BigText = styled.div`
font-size: 20px;
font-size: 25px;
`

const SmallText = emoStyled('div')`
Expand All @@ -27,15 +27,34 @@ const indirectStyled = {
DE: emoStyled('div')`border: 1px solid #F00`,
}

const Async = React.lazy(() => import('./Async'))

const aNumber = 100500

const OtherComponent = () => <span>test 3</span>

const Memo = React.memo(() => (
<div>
[mem <OtherComponent />
<Counter /> memo]
</div>
))

const App = () => (
<h1>
<BigText>1.Hello, world! {aNumber} </BigText>
<BigText>
1.Hello, world! {aNumber} <Counter />
</BigText>
<br />
<SmallText>2.Hello, world.</SmallText>
<SmallText>
2.Hello, world <Counter />.
</SmallText>
<br />
<Counter />
<Memo a1 a2 />
<React.Suspense fallback="loading">
<Async />
</React.Suspense>
<indirect.element />
<indirectStyled.DS>
{' '}
Expand Down
8 changes: 8 additions & 0 deletions examples/styled-components/src/Async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react'
import Counter from './Counter'

export default () => (
<div>
async test <Counter />
</div>
)
2 changes: 2 additions & 0 deletions examples/styled-components/webpack.config.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
entry: ['./src/index'],
mode: 'development',
devtool: 'none',
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
Expand Down
Loading