Local update in the component that uses store causes an extra update from store #638
-
SummaryComponent receives an extra update from the store when its local state changed even though the field it uses from the store never changed. This is observed in valtio + react usage case and reproducible for both react v16-17 and react v18. With one difference: React 18 optimizes the double update and blocks the one(I guess) When following code block is used in the component, you see double commit and double render logs for react v.16.13(also applicable for v17) case and single commit and double render logs for react v18.2 case
Link to reproductionPlease open console and observe logs and profile the app to see an extra update store triggers for both cases. Note that both examples above are NOT in |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 13 replies
-
This is expected behavior. If it renders from parent or state, in this case The difference between react 18 and pre-18 is also expected. It's because react 18 comes with automatic batching. To get the same behavior in pre-18, one would need to use |
Beta Was this translation helpful? Give feedback.
This is expected behavior. If it renders from parent or state, in this case
useState
, we need to run the render and commit, becausebool
in this case may change the result.The difference between react 18 and pre-18 is also expected. It's because react 18 comes with automatic batching. To get the same behavior in pre-18, one would need to use
unstable_batchedUpdates
.