You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Hooks bug it re-renders when calling setX for the same value #14810
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
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.
Reacted by Mark Erikson, Brandon Dail, Matt Hamlin, Arhan Kundu, xu yang, Nothing, Kwan Hung, HO and Edward Lai
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??
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:
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
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
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!
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.
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.
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:
Activity
sophiebits commentedon Feb 9, 2019
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 callsetCount(c => c + 1); setCount(count);with two updates that "cancel out", that isn't known until the render function is called.wood1986 commentedon Feb 10, 2019
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??See https://codesandbox.io/s/13zn77kq2l
sophiebits commentedon Feb 10, 2019
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:
https://codesandbox.io/s/jv7lxyq253
wood1986 commentedon Feb 10, 2019
Your answer is very important to me. Thanks!!
quocvan1311 commentedon Aug 26, 2019
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
aaronwng commentedon Apr 1, 2021
I am also curious why it behave like this
majidmadadi commentedon Jul 21, 2021
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!
Vsnoy commentedon Aug 4, 2022
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.
Vsnoy commentedon Aug 4, 2022
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.
codersjj commentedon Aug 3, 2023
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: