First component loses state on first hot reload
See original GitHub issueIf I modify a functional component, on the first hot reload, the first component loses its internal state. Subsequent reloads are OK. It seems to depend on the order the components are imported in, so may be related to #455. Changing the order of imports of FunctionDefault
and FunctionNamed
changes which one is affected, but one of them always is.
Steps to reproduce:
- Apply the diff below to the current git repository (319e650147c74127c7a4d92328a692f5233efc15). This adds some state to the two functional components, and deletes the class components.
- Start the dev server with
yarn start
- Load the page in the browser, and click the button a few times to modify the state.
- Modify the functional components, for example:
sed -E 's/more/less/g' -i ./*.tsx
Patch
diff --git a/examples/typescript-with-tsc/src/App.tsx b/examples/typescript-with-tsc/src/App.tsx
index c7def2a..dbd820a 100644
--- a/examples/typescript-with-tsc/src/App.tsx
+++ b/examples/typescript-with-tsc/src/App.tsx
@@ -9,8 +9,6 @@ const LazyComponent = lazy(() => import('./LazyComponent'));
function App() {
return (
<div>
- <ClassDefault />
- <ClassNamed />
<FunctionDefault />
<FunctionNamed />
<Suspense fallback={<h1>Loading</h1>}>
diff --git a/examples/typescript-with-tsc/src/FunctionDefault.tsx b/examples/typescript-with-tsc/src/FunctionDefault.tsx
index f886ae3..8022beb 100644
--- a/examples/typescript-with-tsc/src/FunctionDefault.tsx
+++ b/examples/typescript-with-tsc/src/FunctionDefault.tsx
@@ -1,5 +1,12 @@
+import { useState } from "react";
+
function FunctionDefault() {
- return <h1>Default Export Function</h1>;
+ let [state, setState] = useState(1);
+
+ return (<>
+ <h1>Default Export Function{"!".repeat(state)}</h1>
+ <button onClick={() => setState(state => state + 1)}>more</button>
+ </>);
}
export default FunctionDefault;
diff --git a/examples/typescript-with-tsc/src/FunctionNamed.tsx b/examples/typescript-with-tsc/src/FunctionNamed.tsx
index b807a43..35b1127 100644
--- a/examples/typescript-with-tsc/src/FunctionNamed.tsx
+++ b/examples/typescript-with-tsc/src/FunctionNamed.tsx
@@ -1,3 +1,10 @@
+import { useState } from "react";
+
export function FunctionNamed() {
- return <h1>Named Export Function</h1>;
+ let [state, setState] = useState(1);
+
+ return (<>
+ <h1>Named Export Function{"!".repeat(state)}</h1>
+ <button onClick={() => setState(state => state + 1)}>more</button>
+ </>);
}
Observed result
The page updates, but the state of the first component is lost (i.e. reset to the initial state). The second component maintains its state. This only occurs on the first hot reload, subsequent modifications to the components correctly preserve state.
Expected result
Both functional components should maintain their state after the modification causes a hot reload.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
React Redux state is lost at page refresh - Stack Overflow
My personal advice for hot reloading is using this: React hot loader, this prevents page reload and only switch out the altered chunk....
Read more >5 Methods to Persisting State Between Page Reloads in React
1. Using LocalStorage — Class Components. One of the straightforward options is to use localStorage in the browser to persist the state. Let's ......
Read more >Methods closure is lost after hot reload · Issue #978 - GitHub
Going first to "fix" RHL, "back to 4.1.2" behavior (just dont fall), then - implement a new way to update component properties.
Read more >Hot Reloading in React - Medium
React Hot Loader was my first popular open source project and, ... component files without losing the state or unmounting the components.
Read more >Set up React Hot Loader in 10 minutes - LogRocket Blog
The reason we lose the state can be understood by first ... setup react-hot-loader for our React application so that its components can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey, thanks for the debugging. I’ve identified a fix!
@pmmmwh So the problem is
This code is called too late. Therefore react-refresh missed the first component signature