-
Notifications
You must be signed in to change notification settings - Fork 801
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
React context lost on reload when loading it with useContext hook #1207
Comments
Hooks are not quite well tested yet. import {setConfig} from 'react-hot-loader';
setConfig({disableHotRenderer: true}); |
My setup is as follows: plugins: [new webpack.HotModuleReplacementPlugin()], and {
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /(node_modules)/,
options: {
cacheDirectory: true,
plugins: ['react-hot-loader/babel'],
},
}, I have tried using the |
Then I will ask you to create a repo to reproduce a (exactly yours) problem. PS: And look like you are not using hot-patch
|
I think I was able to repro this. I did create-react-app, ejected, added hot-loader, and added a simple context and consuming component: https://github.com/yang/hot-cra-context-repro Here's a video, where I add a period to App.js—you can see from the log statement in the consuming component that it renders twice, first printing 0 (the reset value) before printing 2 (the value it should be): https://www.youtube.com/watch?v=8b6pVEERdkg Misc info:
|
A new ways to get the context, like For now, the best solution is to |
Tested it in different ways - it should just fail to |
Edit: Solution to my specific issue as suggested by the next comment. I am too loosing the context value on Hot Reload (using the useContext() hook). This is my relevant code. PS: In the index.js you also see a snippet to silence the annoying [HRM] console.logs in the browser console. Found that somewhere on github. // index.js
const render = Component => (
ReactDOM.render(
<AppContainer>
<Component />
</AppContainer>,
document.getElementById('root')
)
)
render(App)
if (module.hot) {
module.hot.accept('./App', () => { render(App) })
}
// This is a workaround used alongside the webpack-dev-server hot-module-reload feature
// - it's quite chatty on the console, and there's no currently no configuration option
// to silence it. Only used in development.
// Prevent messages starting with [HMR] or [WDS] from being printed to the console
(function(global) {
var console_log = global.console.log
global.console.log = function() {
if (!(
arguments.length == 1 &&
typeof arguments[0] === 'string' &&
arguments[0].match(/^\[(HMR|WDS)\]/)
)) {
console_log.apply(global.console,arguments)
}
}
})(window) // App.js
import React, {useState, useEffect} from 'react'
import { hot } from 'react-hot-loader'
function App() {
return(<div>Hello World</div>)
}
export default hot(module)(App) |
What would happen if in your ReactDOM.render(<App/>, document.getElementById('root'))
|
I ran into the same and was able to fix the problem, but it contradicts your documentation. Details
Configindex.tsxrequire('@babel/polyfill')
const React = require('react')
const App = require('./App').default
const { render } = require('react-dom')
render(<App />, document.getElementById('root'))
export {} App.tsximport { hot } from 'react-hot-loader/root'
import React from 'react'
import { Provider } from 'react-redux'
import { Router } from 'react-router-dom'
import history from './history'
import Routes from './getRoutes'
import store from './store'
import { authenticateRoute, readToken, readRegToken } from './routeActions'
import ApolloProvider from './ApolloProvider'
import PageviewTracker from './components/utils/PageviewTracker'
const App = () => (
<ApolloProvider>
<Provider store={store}>
<Router history={history}>
<PageviewTracker>
<Routes
auth={authenticateRoute(store)}
readToken={readToken(store)}
readRegToken={readRegToken(store)}
/>
</PageviewTracker>
</Router>
</Provider>
</ApolloProvider>
)
export default hot(App) Unlike in @Mykybo's case, we ran into the lost context bug in a class component (consumer). We logged out the context value in the provider and the consumer as well. The provider's value was what we expected, but the consumer only got SolutionBy removing Do you happen to have some idea why this action solved our problem, @theKashey? |
Removing babel plugin is not a way to solve the problem. Actually without it (or webpack plugin) context should always be lost on update. Something is not right here. |
🙀"Oh fuck"🙀 |
So #1306 would fix the problem, I would release the fix tomorrow, after another check. |
@theKashey Thank you! 🙂 |
Update Context Provider, fixes #1207
4.12.9 has been released. Working with and without react-hot-dom patch. Was broken 20 days ago(4.11.0), and honestly the initial problem, and the last problem are not similar, even have roughly the same effect. |
I'm getting this issue running |
No, that's not expected, and RHL does not control this hook. As a result - the only way to switch back to the initial state - remount a component. |
Yes, that was indeed the problem. I had to remove both |
I'm experiencing this exact same issue on 4.13.0 right now 😕 |
After hot reload, useContext no longer returns the context, I assume the whole context is lost, haven't tried it with a normal consumer.
The text was updated successfully, but these errors were encountered: