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.

useEffect callback never called

See original GitHub issue

I want to report a bug. My problem is that the callback function I pass to useEffect() is never called in my app. I uploaded a minimal app here that reproduces the issue.

As you can see, I render a couple of nested components. After the initial render, I update the component state inside a useEffect() callback. However, this callback is only executed for the first two components, not for the third level component. The result is that the third and subsequent levels are not rendered at all.

I suspect that this is a bug in React and not a misuse on my side because doing any of the following changes will let the component tree render correctly in all levels:

  • Don’t use multiple React roots. If I remove the last (yellow) ReactDOM.render() call, then the second (red) component tree will render correctly.
  • Don’t conditionally render child components. Removing the message !== DEFAULT_MESSAGE check (main.tsx, line 20) causes the component trees to render correctly.
  • Use useLayoutEffect() instead of useEffect().

If you need additional information to reproduce the issue or have any questions, let me know. I’d like provide any help I can!

Issue Analytics

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

github_iconTop GitHub Comments

11reactions
vkurchatkincommented, Oct 11, 2019

Here is somewhat minimal reproduction:

function useTest() {
  const [effect, setEffect] = useState(false);
  useEffect(() => {
    setEffect(true);
  }, []);

  return effect;
}

function A() {
  const effect = useTest();

  return (
    <div >
      {"" + effect}
      {effect && <B/>}
    </div>
  );
}

function B() {
  const effect = useTest();

  return (
    <div >
      {"" + effect}
      {effect && <C/>}
    </div>
  );
}


function C() {
  const effect = useTest();

  return (
    <div >
      {"" + effect}
    </div>
  );
}

function Other() {
  useEffect(() => {}, []);
  return null;
}


ReactDOM.render(
  <A/>,
  document.querySelector("#root1")
);

ReactDOM.render(
  <Other/>,
  document.querySelector("#root2")
);

Some observations:

  • If you change the order of render()s, it works
  • If you use useLayoutEffect, it works
  • If Other doesn’t call useEffect, it works
  • If the second render() is delayed, it works
  • If B or C are rendered unconditionally, it works
  • If setEffect is delayed, it works
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Why useEffect doesn't call callback function if state ...
Every time your component renders, useEffect will be called because you do not provide an array as a second parameter. If you read...
Read more >
Don't useEffect as callback! - Johannes Kettmann
In this blog post, we'll have a look at a common misuse of the useEffect hook. It doesn't cause an error, but it...
Read more >
Rules of Hooks - React
Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any...
Read more >
Avoiding useEffect with callback refs - TkDodo's blog
And that is where useCallback comes in, because that is how we ensure a function is not needlessly created. Maybe that's why they...
Read more >
Your Guide to React.useCallback() - Dmitri Pavlutin
The first problem is that useCallback() hook is called every time MyComponent renders. That reduces the render performance already. The second ...
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