Skip to content
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

Bug: useReducer runs the queued updates with new props #17953

Closed
a-c-sreedhar-reddy opened this issue Feb 1, 2020 · 18 comments · Fixed by #22445
Closed

Bug: useReducer runs the queued updates with new props #17953

a-c-sreedhar-reddy opened this issue Feb 1, 2020 · 18 comments · Fixed by #22445

Comments

@a-c-sreedhar-reddy
Copy link

React version: 16.8.0

Steps To Reproduce

Link to code example:
https://codesandbox.io/s/usereducer-wfcmq Link to codesandbox.

The current behavior

Initially step is 0. So for every step react does not rerender due to this https://github.com/facebook/react/blob/master/packages/react-reconciler/src/ReactFiberHooks.js#L1347. But state updates are queued. So when step changes all the state updates are done and hence creates a bug by adding the new step prop.

https://overreacted.io/a-complete-guide-to-useeffect/#why-usereducer-is-the-cheat-mode-of-hooks

@a-c-sreedhar-reddy a-c-sreedhar-reddy added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Feb 1, 2020
@a-c-sreedhar-reddy a-c-sreedhar-reddy changed the title Bug: Calculating eager state from a reducer and not re rendering if the state does not change causes bug in this example. Bug: useReducer runs the queued updates with new props Feb 1, 2020
@vkurchatkin
Copy link

I’m not 100% sure, but I think that reducer is supposed to be pure, i.e. you can’t use props, otherwise no guarantees are provided

@a-c-sreedhar-reddy
Copy link
Author

a-c-sreedhar-reddy commented Feb 1, 2020

I think since prop is a constant, won't reducer be pure?

@markerikson
Copy link
Contributor

My understanding is that this is both a result of how the reducer function is being defined and capturing local values, and actually entirely intentional behavior.

@a-c-sreedhar-reddy
Copy link
Author

But if the prop is changed then previous actions which are queued but not applied since they were giving the same state are being applied on the new reducer.

@markerikson
Copy link
Contributor

As I said, per Dan's post, this appears to be intentional, and not a bug.

@gaearon
Copy link
Collaborator

gaearon commented Mar 16, 2020

Is this the same problem as #15088 (comment)?

@a-c-sreedhar-reddy
Copy link
Author

a-c-sreedhar-reddy commented Mar 17, 2020

yeah @gaearon. Is it a bug or feature?

@gaearon
Copy link
Collaborator

gaearon commented Mar 17, 2020

I think it's a bug.

@a-c-sreedhar-reddy
Copy link
Author

a-c-sreedhar-reddy commented Mar 17, 2020

Why are the updates queued when it is known that no state has been changed?

@gaearon
Copy link
Collaborator

gaearon commented Mar 17, 2020

Because it is a bug?

@stale
Copy link

stale bot commented Jun 17, 2020

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Jun 17, 2020
@a-c-sreedhar-reddy
Copy link
Author

a-c-sreedhar-reddy commented Jun 17, 2020

bump. Exists in 16.13.1

@stale stale bot removed the Resolution: Stale Automatically closed due to inactivity label Jun 17, 2020
@gaearon gaearon added Component: Hooks Type: Bug and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Jul 9, 2020
@sarathps93
Copy link

sarathps93 commented Aug 3, 2020

Hi @gaearon Shall I work on this if it is ok to?

@a-c-sreedhar-reddy
Copy link
Author

@sarathps93 How can this be fixed can you explain briefly please?

@sarathps93
Copy link

@a-c-sreedhar-reddy I need to start debugging to find out the root cause which I haven't started yet. If no one else is currently working on it, I can give it a shot. If I am able to find a fix for it, I will let you know.

@Gautam-Arora24
Copy link

Seems like no one is working on this one. Can I work on this issue?

@csr632
Copy link

csr632 commented May 22, 2021

I have created a simpler codesandbox to reproduce this bug:
https://codesandbox.io/s/reducer-trigger-unnecessary-lhqnc?file=/src/index.js

The workaround is: don't bail out the reducer (by returning prevState). Instead, return { ...prevState }. So that this action will not be processed again in the next render.

@zxh19890103
Copy link

mark.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants