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.

React.StrictMode combined with useState causes component to render twice

See original GitHub issue

Do you want to request a feature or report a bug? Bug (maybe)

What is the current behavior? If wrapped in React.StrictMode and a function component contains a call to useState, the function (render) is called twice - even if the state setter is never called.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. https://codesandbox.io/s/lyw9514j4q (please see console)

What is the expected behavior? I would expect it to only render once.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? 16.8.3 in Chrome on macOS Mojave; requires hooks so not tested with previous versions.

This may be related to https://github.com/facebook/react/issues/12961 - but note that nowhere am I setting state (only initializing it).

If this is expected behavior please feel free to close. Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:32
  • Comments:22 (5 by maintainers)

github_iconTop GitHub Comments

153reactions
gaearoncommented, Mar 9, 2019

It’s an intentional feature of the StrictMode. This only happens in development, and helps find accidental side effects put into the render phase. We only do this for components with Hooks because those are more likely to accidentally have side effects in the wrong place.

8reactions
sdennett55commented, Apr 10, 2020

Sorry, I know this was closed, but you say

We only do this for components with Hooks…

but if your component uses a class – even if it’s stateless – it also renders twice. Threw me for a loop today trying to figure out why my component wasn’t working properly.

export default class App extends React.Component {
  render() {
    console.log('this logs twice with React.StrictMode');
    return null;
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does useState cause the component to render twice on ...
Your App component is rendered within React.StrictMode which is what causes your code to run in strict mode and in strict mode the...
Read more >
React Components rendered twice — any way to fix this?
StrictMode . It only happens in development mode and should help to find accidental side effects in the render phase. Let's find out...
Read more >
5 Ways to Avoid React Component Re-Renderings
Since we are using useRef() , it will only cause a single render. However, if we use useState() , it will cause 2...
Read more >
How to stop useEffect from running twice on mount ... - YouTube
Learn how to stop useEffect from running twice on mount or first render in React. The new React 18 strict mode runs useEffect...
Read more >
Strict Mode - React
By intentionally double-invoking methods like the component constructor, strict mode makes patterns like this easier to spot. Note: In React 17, React ......
Read more >

github_iconTop Related Medium Post

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 Hashnode Post

No results found