question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

First component loses state on first hot reload

See original GitHub issue

If 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:

  1. Apply the diff below to the current git repository (319e650147c74127c7a4d92328a692f5233efc15). This adds some state to the two functional components, and deletes the class components.
  2. Start the dev server with yarn start
  3. Load the page in the browser, and click the button a few times to modify the state.
  4. 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:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
pmmmwhcommented, Sep 13, 2021

Hey, thanks for the debugging. I’ve identified a fix!

0reactions
Jack-Workscommented, Sep 12, 2021

@pmmmwh So the problem is

webpack_require.$Refresh$.signature = webpack_require.$Refresh$.runtime.createSignatureFunctionForTransform;

This code is called too late. Therefore react-refresh missed the first component signature

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found