The useEffect Hook Signature
What you'll learn: How to write the basic structure of the useEffect hook to run side effects in your components.
Now that we understand why side effects can't run during render, let's look at how we actually write them. React gives us the useEffect hook specifically for this purpose.
The Basic Syntax
The useEffect hook takes a function as its first argument. This function contains all the side effect code you want to run after rendering:
import { useEffect } from 'react';
function MyComponent() {
useEffect(() => {
// Your side effect code goes here
console.log('Component rendered!');
});
return <div>Hello</div>;
}
That's it — the core signature is just useEffect plus a function.
What Happens
When you call useEffect with a function:
- React registers that function during the render phase
- After the component finishes rendering and the DOM updates
- React runs your function during the effects phase
Think of it like leaving a note for React: "After you finish painting the screen, please run this code for me."
The Function You Pass
The function you give to useEffect is where all your side effect logic lives. This is where you'll:
- Fetch data from APIs
- Subscribe to events
- Update the document title
- Log analytics
- Set up timers
React calls this function after the render, keeping your render phase pure and predictable.
Key Takeaway: useEffect takes a function containing your side effect code — React runs that function after rendering completes, not during it.