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

The Cleanup Function Signature

Learning the syntax: return a function from your effect that contains cleanup logic, with no parameters or return value.

The Cleanup Function Signature

You'll learn the exact syntax for returning a cleanup function from your effect.

The Basic Syntax

When you need to perform cleanup in a useEffect, you return a function from inside the effect. That's it. This returned function is your cleanup function, and React will call it automatically when needed.

The signature is simple:

useEffect(() => {
  // Your effect logic here

  return () => {
    // Your cleanup logic here
  };
}, [dependencies]);

The cleanup function takes no parameters and returns nothing (or more precisely, its return value is ignored). It's just a plain function that executes cleanup code.

Why This Signature?

Think of it like subscribing to a newsletter. The effect is you signing up, and the cleanup function is you unsubscribing. You don't need any information to unsubscribe — you just do it. Similarly, the cleanup function doesn't need arguments because it already has access to everything from the effect's closure.

useEffect(() => {
  console.log('Effect running');

  return () => {
    console.log('Cleanup running');
  };
}, []);

What NOT to Do

Don't try to pass parameters to the cleanup function or return values from it:

// ❌ Wrong - cleanup doesn't take parameters
return (someParam) => { /* ... */ };

// ❌ Wrong - cleanup return value is ignored
return () => { return 'something'; };

Key Takeaway: The cleanup function signature is dead simple: return a parameterless function from your effect that contains cleanup code. React handles calling it at the right time.