Skip to content

Bug: useReducer rendering behaviour in React 18  #26131

Closed as not planned
Closed as not planned
@sshubham681

Description

@sshubham681

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution: StaleAutomatically closed due to inactivityStatus: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions