Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: using useState without changing the state generate a useless rerender #18761

Closed
Sharcoux opened this issue Apr 28, 2020 · 10 comments
Closed
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@Sharcoux
Copy link

Sharcoux commented Apr 28, 2020

When using useState in a component, this component will rerender twice, whereas nor the state nor the props did change.

React version: 16.13.1

Steps To Reproduce

  1. Create a dummy functionnal component
  2. Create a state with useState

Link to code example: codesandbox

import React from "react";

let count = 0;

const App = () => {
  const [a] = React.useState("render");
  count++;
  console.log(a, count);
  return <div>{count}</div>;
};

export default App;

The current behavior

Logs are:
render 1
render 2

The expected behavior

render 1

@Sharcoux Sharcoux added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Apr 28, 2020
@mykolarositskyi
Copy link

Same problem. When removing React.StrictMode it solves the problem

@nyz123
Copy link

nyz123 commented Apr 29, 2020

Same problem. When removing React.StrictMode it solves the problem

When removing useState and useEffect it solves the problem,too.

@Sharcoux
Copy link
Author

Same problem. When removing React.StrictMode it solves the problem

When removing useState and useEffect it solves the problem,too.

Removing the component solves the problem too xD

@nyz123
Copy link

nyz123 commented Apr 29, 2020

Same problem. When removing React.StrictMode it solves the problem

When removing useState and useEffect it solves the problem,too.

Removing the component solves the problem too xD

haha~, you make me laugh...

@robertvansteen
Copy link

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.

Source: #15074 (comment)

@Sharcoux
Copy link
Author

Ok, my bad then. I didn't know at all about this feature. Should strict mode log a message in develpment to warn about this or anything? I think it is hard for devs to discover about the source of what they may experience in the current state of things.

@eddiecooro
Copy link

@Sharcoux I think Sebastian already worked on this part: PR.
But as a general rule, React doesn't promise anything about when and how many times it calls our function. So it might call the render function multiple times even outside of the StrictMode.

@macmaster
Copy link

macmaster commented May 3, 2020

Cool, thanks for sharing. I learned something new.

Side effects in render-phase lifecycle methods will break when enabling Concurrent mode.

https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects

@rachelnabors
Copy link

I can confirm this is fixed in #18547 and the fix will be included in React 17. To be clear, the component will still run twice, but the console.log will be disabled on the second run.

@gaearon
Copy link
Collaborator

gaearon commented Mar 30, 2022

To clarify, there was no bug here, and so nothing was fixed. The behavior is as intended: StrictMode calls your component function twice to help find accidental mutations and side effects.

@facebook facebook locked as resolved and limited conversation to collaborators Mar 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

8 participants