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.

Hot update blows up when adding/removing hooks in React file

See original GitHub issue

Hi,

I’m working on a legacy project and trying to hack modern React dev tool chain into the environment. I’m using webpack-hot-middleware and following the setup in this example. The basic HMR works well and I could see my changes reflected in the browser.

image

And my test React file looks like this:

const HelloWorld = () => {
    const [count, setCount] = React.useState(0);
    return (
        <div>
            <button onClick={() => setCount(count+1)} >plus</button>
            <div>{count}</div>
        </div>
    );
};

export default HelloWorld;

However hot update blows up when I add a hook to the react file:

const HelloWorld = () => {
    const [count, setCount] = React.useState(0);
    const [text, _] = React.useState("");
    return (
        <div>
            <button onClick={() => setCount(count+1)} >plus</button>
            <div>{count}</div>
            <div>{text}</div>
        </div>
    );
};

If I make the change and save then it will pop up this hooks error: image

I’m clear of the rule of hooks and why this error would pop up in normal development. However I don’t understand why it would show up for HMR in this case. and I don’t see the error when in your example or in any other HMR repos.

It would be hard for me to duplicate my environment because of the complexity and I do apologize for that. But here is what I could tell how it’s different from the other runtime

  1. React package is marked as “external” in webpack config and loaded during runtime
  2. The react part runs in an Iframe.

Now that I have no clue of why I’m getting the hooks error. Do you by any chance have any clue why this is happening?

Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pmmmwhcommented, Nov 8, 2021

I’ve been looking at this but do not have any progress yet. I believe it is likely still related to injection order and Babel processing being incorrect.

1reaction
rockcs1992commented, Oct 16, 2021

@gaearon @pmmmwh Please take a look at the minimal example ^

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple calls to state updater from useState in component ...
I'm trying React hooks for the first time and all seemed good until I realised that when I get data and update two...
Read more >
React Hook Pitfalls by Kent C. Dodds
In this talk I teach React from scratch in a single index.html file with no magic up my sleeves. We start with a...
Read more >
Bug: React Dev Tools crashes with Hookstate hooks #20609
I am using Hookstate to manage the states of my components. Dev Tools shows some errors when using the Hookstate's useState hook more...
Read more >
react-hot-loader | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
How to Optimize React Hooks - Bits and Pieces
The first step is to let React in on a secret again: “If the props of our Grid haven't updated, don't bother re-rendering...
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