Skip to content

Hooks: Calling setState with the SAME value multiple times, evaluates the component function up-to 2 times #17474

Closed

Description

@rwanyoike

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Using the State hook, calling setXYZ() with the same value multiple times causes the component function to be evaluated more than once (up-to 2 times). I don't mean re-rendered, but a re-evaluation of its function.

Scenario 1 (OK):

  • the component function is evaluated, initial render useState(1)
  • click button => setState(1)
  • the component function is not re-evaluated. ✔️
  • click button again => setState(1)
  • the component function is not re-evaluated.
  • click button again => setState(1)
  • the component function is not re-evaluated.
  • etc.

Scenario 2:

  • the component function is evaluated, initial render useState(1)
  • click button => setState(2)
  • the component function IS re-evaluated, component re-rendered ✔️
  • click button again => setState(2)
  • the component function IS evaluated AGAIN ❎
  • click button again => setState(2)
  • the component function is not re-evaluated.
  • click button again => setState(2)
  • the component function is not re-evaluated.
  • etc.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

https://jsfiddle.net/qf103zvj/

What is the expected behavior?

Just as Scenario 1, I'd expect ONLY one re-evaluation of the component function.

Scenario 2:

  • the component function is evaluated, initial render useState(1)
  • click button => setState(2)
  • the component function IS re-evaluated, component re-rendered ✔️
  • click button again => setState(2)
  • the component function is not re-evaluated. ✔️
  • click button again => setState(2)
  • the component function is not re-evaluated.
  • etc.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

Chrome 78, Firefox 70, React 16; Both macOS, Linux. Unsure about previous versions of React.

Activity

  1. changed the title [-]Hooks: Calling setState with the SAME value multiple times, evaluates the component function up-to 2 times?[/-] [+]Hooks: Calling setState with the SAME value multiple times, evaluates the component function up-to 2 times[/+] on Nov 27, 2019
  2. AjaxSolutions commented on Nov 27, 2019

    @AjaxSolutions

    Interesting discovery. This certainly doesn't look like a useful feature.

  3. sebmarkbage commented on Dec 3, 2019

    @sebmarkbage
    Contributor

    This is a known quirk due to the implementation details of concurrency in React. We don't cheaply know which of two versions is currently committed. When this ambiguity happens we have to over render once and after that we know that both versions are the same and it doesn't matter.

  4. christiaanwesterbeek commented on Dec 18, 2019

    @christiaanwesterbeek

    I encountered the same thing while trying to understand React hooks better.

    Check out this fiddle, and open up the Console tab. https://jsfiddle.net/devotis/jd12g7y4/

    In that fiddle, the App function is run one extra time after calling setValue with the same value, but the result of the last function call, is not being sent to the dom. Also notice how useEffect is not calling its function after that last function call (which makes sense, because there wasn't any effect in the dom with that last function call).

    I do not consider this a bug. Just something I didn't expect at the time. To be precise; I didn't expect the App function to be called the last time (when setValue was called with the same value). The explanation by @sebmarkbage makes sense though. Is this behaviour documented somewhere?

  5. stale commented on Mar 18, 2020

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.

  6. added
    Resolution: Stale
    on Mar 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution: Stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions