-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
useReducer: dispatched actions are reduced twice #14360
Comments
This does look like a bug. Want to dig into it? |
I'd love to, but I am not sure I understand the implications on messing with the action queue, there are quit a lot of special cases and as I wrote that was my first read of any react implementation code :) Also it might take me a week or so, as I am pretty busy at the moment. |
Actually I was wrong, the higher priority action should be reduced twice, the error is in I have a test that reproduced my bug and is fixed by correcting |
It was broken because `cloneHook` assigned `memoizedState` instead of `baseState` from the original hook to `baseState` of the clone.
Nice find :-) |
* Fixes #14360 and adds a test for mixed priority dispatches. It was broken because `cloneHook` assigned `memoizedState` instead of `baseState` from the original hook to `baseState` of the clone. * tweak comments
* Fixes facebook#14360 and adds a test for mixed priority dispatches. It was broken because `cloneHook` assigned `memoizedState` instead of `baseState` from the original hook to `baseState` of the clone. * tweak comments
* Fixes facebook#14360 and adds a test for mixed priority dispatches. It was broken because `cloneHook` assigned `memoizedState` instead of `baseState` from the original hook to `baseState` of the clone. * tweak comments
Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
There are cases where actions dispatched by
useReducer
's dispatch function are reduced twice (in concurrent mode).As far as I can tell from my first quick read of the code of react-dom (so I might be totally wrong!), this happens, when a higher priority action is added to a queue that already contains lower priority actions.
On the first run, that is caused by for example an onClick handler (higher priority), the dispatched action is appended to the queue. On processing the queue the lower priority actions (for example from setInterval events) are ignored and the "onClick action" is reduced, but stays on the end of the queue, so that on the next time the queue is processed, that action gets reduced a second time.
See a testcase at:
https://codesandbox.io/s/nxq3w7rwl
Pressing the button increments the button (most of the time not always, depending if 'TICK' actions are in the queue) by two.
What is the expected behavior?
I'd expect a dispatched action to be only reduced once.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Tested with React 16.7.0-alpha.2 on Chrome (70.0.3538.110) and Safari (12.0.1) both on macOs
The text was updated successfully, but these errors were encountered: