Skip to content

Hooks bug it re-renders when calling setX for the same value #14810

Closed

Description

@wood1986

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

What is the current behavior?
out

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:

See https://codesandbox.io/s/x31lqr09zq

What is the expected behavior?
If I click "count = count", it should not execute the function again

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
16.8.1. All browsers have the same issue

Activity

  1. sophiebits commented on Feb 9, 2019

    @sophiebits
    Collaborator

    This may be unintuitive, but it's the expected behavior. See the docs: https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-state-update. When you pass the same state value, React will call the component render function but it won't recurse to the children or run any effects.

    If you're curious, this is because the updates are not processed until the render phase. For example if you write setCount(c => c) (a no-op state update), that state updater function is called during the render (we do this for consistency with useReducer, which does this in order to have access to the latest props). So there's no way to skip it. Similarly, if you were to call setCount(c => c + 1); setCount(count); with two updates that "cancel out", that isn't known until the render function is called.

  2. wood1986 commented on Feb 10, 2019

    @wood1986
    Author

    Hey @sophiebits, Thanks for the explanation.

    My following question is not about this issue. It is about the comparison algorithm for bailing out of a state update. Why not using the shallowEqual??

    2

    See https://codesandbox.io/s/13zn77kq2l

  3. sophiebits commented on Feb 10, 2019

    @sophiebits
    Collaborator

    shallowEqual would be slower and not always what you want. However if you want shallowEqual behavior you can write it yourself using something like this:

    function useStateWithShallowEqual(initialValue) {
      let [value, setValue] = useState(initialValue);
      let setWithShallowEqual = useCallback(function (newValue) {
        setValue(val => {
          if (typeof newValue === 'function') {
            newValue = newValue(val);
          }
          if (shallowEqual(val, newValue)) {
            return val;
          } else {
            return newValue;
          }
        });
      }, []);
      return [value, setWithShallowEqual];
    }

    https://codesandbox.io/s/jv7lxyq253

  4. wood1986 commented on Feb 10, 2019

    @wood1986
    Author

    Your answer is very important to me. Thanks!!

  5. quocvan1311 commented on Aug 26, 2019

    @quocvan1311

    Hi @sophiebits, according to your answer, I've figured out that it only causes an extra re-render when value is changed then be updated with the same again.
    Look at my sample code at https://codesandbox.io/s/aged-architecture-jhm6o
    If you click the 2nd for the first time, no re-render trigger.
    But if you click the 1st button, then click the 2nd button again, it causes an extra re-render.
    And by the way, can you explain more why it need to re-render 1 time when it updates with the same value. Thanks in advance

  6. aaronwng commented on Apr 1, 2021

    @aaronwng

    Hi @sophiebits, according to your answer, I've figured out that it only causes an extra re-render when value is changed then be updated with the same again.
    Look at my sample code at https://codesandbox.io/s/aged-architecture-jhm6o
    If you click the 2nd for the first time, no re-render trigger.
    But if you click the 1st button, then click the 2nd button again, it causes an extra re-render.
    And by the way, can you explain more why it need to re-render 1 time when it updates with the same value. Thanks in advance

    I am also curious why it behave like this

  7. majidmadadi commented on Jul 21, 2021

    @majidmadadi

    This may be unintuitive, but it's the expected behavior. See the docs: https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-state-update. When you pass the same state value, React will call the component render function but it won't recurse to the children or run any effects.

    If you're curious, this is because the updates are not processed until the render phase. For example if you write setCount(c => c) (a no-op state update), that state updater function is called during the render (we do this for consistency with useReducer, which does this in order to have access to the latest props). So there's no way to skip it. Similarly, if you were to call setCount(c => c + 1); setCount(count); with two updates that "cancel out", that isn't known until the render function is called.

    My question is why react doesn't couse any render when we set and initial value to setState() function? Consider the following function:
    function Test() { const [id, setId] = useState(1); console.log("rendering..."); return <button onClick={() => setId(1)}>Click</button>; }
    It doesn't render when we click the button. According to your answer it should cause a render by the first click. I think this is a big inconsistency!

  8. Vsnoy commented on Aug 4, 2022

    @Vsnoy

    Hi @sophiebits, according to your answer, I've figured out that it only causes an extra re-render when value is changed then be updated with the same again. Look at my sample code at https://codesandbox.io/s/aged-architecture-jhm6o If you click the 2nd for the first time, no re-render trigger. But if you click the 1st button, then click the 2nd button again, it causes an extra re-render. And by the way, can you explain more why it need to re-render 1 time when it updates with the same value. Thanks in advance

    I'm also confused about it recently.
    There are few articles on the Internet about it.
    I just find some keywords:update queue、lazy computation、immediate computation.

  9. Vsnoy commented on Aug 4, 2022

    @Vsnoy

    Hi @sophiebits, according to your answer, I've figured out that it only causes an extra re-render when value is changed then be updated with the same again. Look at my sample code at https://codesandbox.io/s/aged-architecture-jhm6o If you click the 2nd for the first time, no re-render trigger. But if you click the 1st button, then click the 2nd button again, it causes an extra re-render. And by the way, can you explain more why it need to re-render 1 time when it updates with the same value. Thanks in advance

    I'm also confused about it recently.
    There are few articles on the Internet about it.
    I just find some keywords:update queue、lazy computation、immediate computation.

  10. codersjj commented on Aug 3, 2023

    @codersjj

    This may be unintuitive, but it's the expected behavior. See the docs: https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-state-update. When you pass the same state value, React will call the component render function but it won't recurse to the children or run any effects.

    If you're curious, this is because the updates are not processed until the render phase. For example if you write setCount(c => c) (a no-op state update), that state updater function is called during the render (we do this for consistency with useReducer, which does this in order to have access to the latest props). So there's no way to skip it. Similarly, if you were to call setCount(c => c + 1); setCount(count); with two updates that "cancel out", that isn't known until the render function is called.

    I see, but what does "the latest props" mean here? Doesn't the reducer in useReducer access the state instead of props? Is my understanding of "the latest state" incorrect?:confused:

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions