Skip to content

QuizNot attempted

Why immutability

Why does obj.count = 1; setState(obj) not re-render?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • React compares state with Object.is; the reference is unchanged, so it bails out
  • React decides whether state changed with Object.is
  • after mutating, obj is the same reference, so setState(obj) bails out.
  • Create a new object: setState({ ...obj, count: 1 }).
  • Not 'primitives only': state can be any value.
  • Not 'throws in strict mode': JS allows mutating a non-frozen object, and StrictMode doesn't freeze state.
  • Not 'delayed re-render': there is no re-render at all.
  • Mutation also breaks memo and time-travel debugging.