Search code examples
reactjsreduxuse-context

would redux trigger re render if a nested property is updated?



export const userSlice = createSlice({
  name: "user",
  initialState: {
    info: {
      dob: null,
    }
  }
})

What if the dob property is updated but the reference that info is pointing to is not changed? Would this cause a component which depends on user.info.dob to re render?


Solution

  • Depends on the data type. If it is primitive - (number, string) yes

    If its an object, then you have to reference (point to another object). It can be as simple as {... prevObject, newProperties }

    But deeply nested can make life miserable. So using a library called Immer is recommended (https://beta.reactjs.org/learn/choosing-the-state-structure)