DEV Community

Aileen Rae
Aileen Rae

Posted on Edited on Originally published at aileenrae.co.uk

TIL: You can watch for nested properties changing in React's useEffect() hook

Today I learned: you can watch for nested properties changing in React's useEffect() hook by using dot notation in the dependency array, like so:

// hook runs whenever someValue.someNestedValue is updated
useEffect(()=> {
      // do something
}, [someValue.someNestedValue])
🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025 🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025

React's useEffect() hook lets you perform a side-effect in a functional component. That is, whenever the component updates, whatever code you put in the useEffect() hook executes.

By default, the hook runs on every component update, which for performance reasons we should avoid:

useEffect(()=> {
    // I run when ANYTHING updates
})
🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025 🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025

But, we can optionally pass a dependency array as a second argument to tell React to only re-run the hook when the specified dependencies update:

useEffect(()=> {
    // I only run when someValue updates
}, [someValue])
🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025 🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025

But what about if you have nested data, say an object containing form data, e.g.

const values = {
  name: 'Aileen',
  age: 26,
  city: 'Edinburgh',
  favoriteFood: 'pizza 🍕'
}
🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025 🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025

What if I want to trigger a side-effect when the user updates just their age, and not when any other value changes?

It seems obvious in hindsight, but you can watch for nested values changing simply by referencing the nested property with dot notation:

useEffect(()=> {
    ageChangeSideEffect(values.age);
}, [values.age])
🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025 🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025

Additionally, we can trigger the useEffect() hook for deeper nested objects if necessary:

useEffect(()=> {
    // do something
}, [someValue.someNestedValue.someDeeplyNestedValue])
🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025 🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025

Top comments (4)

🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025
 
gabrielmlinassi profile image
Gabriel Linassi • • Edited

It's a fine solution if the object property always exists however if the property is not present at some point you get a reference error. My workaround in this scenario is to check if prop exists inside the hook

useEffect(()=> {
if (values?.age) {
ageChangeSideEffect(values.age);
}
}, [values])

🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025
 
abe profile image
Abraham J Gonzalez B •

I think is better to use values?.age as a dependency, to avoid to listen all changes in values.

🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025
 
milichev profile image
Vadym Milichev •

There is no change listening in values because it's an object which is checked for equality by reference. If values is passed to the component as a property and its value is composed as a plain inline object, it will be a new value on each render.

🏆 ยอดนิยม: aileenr/til you can watch for nested properties changing in react s useeffect hook nj - Kapook Update 2025
 
yaireo profile image
Yair Even Or •

Please un-learn what you learned "today":

github.com/facebook/react/issues/1...