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 rendering behaviour in React 18 #26131

Closed
sshubham681 opened this issue Feb 9, 2023 · 9 comments
Closed

Bug: useReducer rendering behaviour in React 18 #26131

sshubham681 opened this issue Feb 9, 2023 · 9 comments
Labels
Resolution: Stale Automatically closed due to inactivity Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@sshubham681
Copy link

sshubham681 commented Feb 9, 2023

import React, { useReducer } from "react";

const initialState = 0;

const reducer = (state, action) => {
switch (action) {
case "INCREMENT":
return state + 1;
case "DECREMENT":
return state - 1;
case "RESET":
return initialState;
default:
return state;
}
};

const UseReducer = () => {
const [count, dispatch] = useReducer(reducer, initialState);
console.log('UseReducer Render')
return (


{count}

<button onClick={() => dispatch("INCREMENT")}>Increment
<button onClick={() => dispatch("DECREMENT")}>Decrement
<button onClick={() => dispatch("RESET")}>Reset

);
};

export default UseReducer;

In react 17, useReducer re-render was working as same as useState does but now after updating version to 18, whenever i click reset button, console.log('UseReducer Render'); is printed

@sshubham681 sshubham681 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Feb 9, 2023
@hhkk28
Copy link

hhkk28 commented Feb 20, 2023

Even with react version 17, the console.log('UseReducer Render') is re-rendered on every state update. Here is the same useReducer behavior in version 17 - Repl-Link and in version 18 Repl-Link I don't think this is a bug.

@mikirejf
Copy link

@hhkk28 There's a difference between v17 and v18 for the "reset" button. The v17 version is working as described in the docs (https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-dispatch):

Bailing out of a dispatch
If you return the same value from a Reducer Hook as the current state, React will bail out without rendering the children or firing effects. (React uses the Object.is comparison algorithm.)

Note that React may still need to render that specific component again before bailing out. That shouldn’t be a concern because React won’t unnecessarily go “deeper” into the tree. If you’re doing expensive calculations while rendering, you can optimize them with useMemo.

Check out the following example: https://codesandbox.io/s/wispy-hooks-0vytuh?file=%2Fsrc%2FApp.js
React should bail out from continuously re-rendering the component (one additional render is expected).

@hhkk28
Copy link

hhkk28 commented Feb 23, 2023

I see, now i understand the issue. According to the PR - #22445 I think this bailout behavior is removed for useReducer.

@mikirejf
Copy link

@hhkk28 Thanks for pointing out the PR. Much appreciated!

@tomdevisser
Copy link

Does that mean this issue can be closed @mikirejf ?

@mikirejf
Copy link

@tomdevisser

Can be closed 👍

@tomdevisser
Copy link

@sshubham681

@tomdevisser

Can be closed 👍

Copy link

github-actions bot commented Apr 9, 2024

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!

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 9, 2024
Copy link

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Stale Automatically closed due to inactivity Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

4 participants