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.

Bug: useMemo hook executes twice

See original GitHub issue

I’m using useMemo hook with an empty dependency array in a component withlazy + Suspense, so I expect the function inside useMemo will be called once, but sometimes the function is called twice.

No StrictMode, no rerenders.

useMemo(() => {
  console.log('useMemo');
}, []);

React version: 18.2.0

I can’t reproduce it with version 17.0.2

Steps To Reproduce

Please, take a look at the simplified example. I could reproduce it on a regular basis after I’ve added setState call inside useMemo. As the issue is hard to reproduce, there is a script that reloads the page until the bug appears.

Link to code example: https://codesandbox.io/s/smoosh-forest-g6ft5o

Pay attention, that function in useEffect was called once, which is expected behavior, but useMemo was called twice.

In the real project, there is no setState call inside useMemo and no warnings, but anyway I meet the issue every 10-20 page reloads.

If I delete lazy it works as expected. If I drop LongComponent it works as expected.

The current behavior

The function passed to useMemo is executed twice despite the empty dependency array, and the component wasn’t rerendered.

console:
  useMemo
  useMemo
  useEffect

The expected behavior

The function passed to useMemo is executed only once.

console:
  useMemo
  useEffect

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
832bb9commented, Aug 31, 2022

Same here, I’m using a dependency injection framework factory method inside useMemo to create one instance of a model inside my component, using react 18 triggers useMemo few times for no reason.

Please check this https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily. You can manually save this instance to ref on very first render (or even lazily).

1reaction
kolserdavcommented, Sep 30, 2022

This is a real useMemo in React 18.2.0 bug. Here is a simple example where useMemo is triggered twice for each dependency value.

https://codesandbox.io/s/compassionate-swartz-wd3j94?file=/src/App.js

https://iili.io/LEGLkg.png

Or is this normal in strict mode?

Yes, I see this is normal https://reactjs.org/docs/strict-mode.html

Read more comments on GitHub >

github_iconTop Results From Across the Web

React memo get magically executed twice [duplicate]
i'm creating a react app with useMemo . The memo uses empty array as the dependency list, so it should be executed once,...
Read more >
React 18 - Avoiding Use Effect Getting Called Twice
For React Hooks in React 18, this means a useEffect() with zero dependencies will be executed twice.
Read more >
You're overusing useMemo: Rethinking Hooks memoization
The argument passed to useState is better called INITIAL_STATE . It's only computed (or triggered) once when the component is initially mounted.
Read more >
useMemo - React Docs
useMemo. useMemo is a React Hook that lets you cache the result of a ... My calculation runs twice on every re-render; My...
Read more >
React hooks... Oops! Part 2 - why does my effect run multiple ...
If any of them has changed since the last render, fn is run again. Even though it sounds pretty simple, there are some...
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