React Component does not re rendering when updating an object in state hooks

Fernanda Akhsanuddin

Why this component does not update when the state updating? But, when the file is updated (like editing and saving the code), the component will re-rendering and show the correct value from the updated state. Here is the code

import { useReducer } from "react";
import "./styles.css";

const init = [
  {
    name: "Car",
    stock: 100
  },
  {
    name: "Truck",
    stock: 50
  }
];

const reducer = (state, action) => {
  if (action.type === "add") { 
    state[0].stock++;
    return state;
  } else { 
    return state;
  }
};


export default function App() {
  const [state, dispatch] = useReducer(reducer, init);
  const addCarStock = () => {
    dispatch({ type: "add" });
  };

  return (
    <div>
      <p>Car Stock : {state[0].stock}</p>
      <button onClick={addCarStock}>Add Car Stock</button>
    </div>
  );
}

You can open this code in sandbox.io

Amila Senadheera

When you do state[0].stock++;. It changes the existing array(you can actually check that array updated by putting a console.log(state); in the first line of reducer function) and when react does rendering it does a shallow comparison (reference checking) and ignores re-render assuming it is the same array (because prev and new state refer to the same instance).

You need to create a new array instance (state) with the changes and return it.

const reducer = (state, action) => {
  if (action.type === "add") {
    return state.map((item,itemIndex) => {
      if(itemIndex === 0){
        return {...item, stock: item.stock + 1}
      } else {
        return item;
      }
    });
  } else {
    return state;
  }
};

Code sandbox => https://codesandbox.io/s/recursing-davinci-iid5h?file=/src/App.js

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React component is rendering but not updating when state is updating

React Hooks: set component state without re-rendering twice

Why does updating state using React Hooks, MomentJS object and interval/timeout not trigger re-render?

component not re-rendering when updating the state in mobx

Component not re-rendering when updating state within map function

React Hooks - setting object not re-rendering the component's data

react custom hook state is not updating when re-rendering

Why is the state not updating inside this react Hooks component?

Component local state not updating with react custom hooks

React hooks: Parent component not re-rendering

React component not re-rendering when state changes

Parent React component not re-rendering on change state in child using react hooks;

React/react hooks: child component is not re-rendering after the state is changed?

updating react component state using this.setState isn't re-rendering the component

How does React re-use child components / keep the state of child components when re-rendering the parent component?

React state updating but not rendering on child component

React Hooks: how to avoid re-rendering of component on another state change?

State not updating in react with a nested object (hooks)

React 16.8 hooks - child component won't re-render after updating parent state

React hooks state is undefined when updating

React not re-rendering child component after updating state var passed as prop

React component not re-rendering on state change

React function component state not re-rendering?

Why is the component fully re-rendering when updating a single state through context?

React hooks - useEffect updating state but the updated state is not re-rendered

Component not re-rendering when state is changed

React Hooks, state not updating

How to prevent child component from re-rendering when using React hooks and memo?

React component create a new instance of the state when re-rendering even the state is not changed