Course contentsShow
← React
Lesson 531 of 2,39615. useEffect FundamentalsFree lesson

Reading Props and State in Effects

See how effects can access the latest props and state from the component's scope.

Reading Props and State in Effects

What you'll learn: How effects can "see" and use the latest props and state values from your component.

Effects Live Inside Your Component

Because useEffect is called inside your component function, it naturally has access to everything in that component's scope — props, state variables, and any other values declared in the function body.

Think of it like this: your effect is just another piece of code in your component. It can "reach out" and grab any values it needs, just like an event handler would.

Accessing Current Values

When your effect runs after the render commits, it uses the props and state values from that specific render:

function Greeting({ name }) {
  const [count, setCount] = useState(0);

  useEffect(() => {
    // This effect can read both props and state
    console.log(`Hello ${name}, count is ${count}`);
  });

  return <button onClick={() => setCount(count + 1)}>Increment</button>;
}

Every time Greeting renders (whether from a prop change or state update), the effect runs and "sees" the current name and count values.

Why This Matters

This access pattern is fundamental to how effects work. You'll use it to:

  • Log current values for debugging
  • Send data to external APIs based on current state
  • Update the document title with current information
  • Synchronize with browser APIs using fresh props

The effect always gets a "snapshot" of the props and state from its render. If they change, the component re-renders, and the effect runs again with the new values.

Key Takeaway: Effects can freely access props and state from their component's scope — they see the values from the render that produced them.