React Hooks: Using useState with an object: how to update

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


When a state variable defined with useState is an object with properties you add / update, it’s somewhat confusing how to update it.

You can’t just update the object, or the component won’t rerender.

A pattern I found involves creating a temporary object with one property, and use object destructuring to create a new object from the existing 2 objects:

const [quizAnswers, setQuizAnswers] = useState({})

...

const updatedValue = {}
updatedValue[quizEntryIndex] = answerIndex
setQuizAnswers({
  ...quizAnswers,
  ...updatedValue
})

The same technique can be used to remove a property:

const copyOfObject = { ...quizAnswers }
delete copyOfObject['propertyToRemove']

setQuizAnswers({
  ...copyOfObject
})

Lessons in this unit:

0: Introduction
1: Introduction to React Hooks
2: How to use the useState React hook
3: useEffect React hook, how to use
4: How to use the useContext React hook
5: How to use the useReducer React hook
6: How to use the useCallback React hook
7: How to use the useMemo React hook
8: How to use the useRef React hook
9: Can I use React hooks inside a conditional?
10: Why does useEffect run two times?
11: ▶︎ Using useState with an object: how to update
12: How to reference a DOM element in React