Skip to content

Side Effect

In one line

A side effect is any operation that affects something outside the component — data fetching, DOM manipulation, subscriptions, logging.

In simple words

In React, a pure component is one that, given the same props and state, always returns the same JSX and doesn't affect anything else. A side effect is anything that breaks this purity — fetching data, writing to localStorage, subscribing to a websocket, manually changing the DOM, logging to the console.

React components should be pure (just compute JSX from props/state). Side effects are handled separately, in useEffect. This separation keeps rendering predictable: React can call your component function any number of times (for reconciliation, concurrent rendering) without worrying about side effects firing unexpectedly.

The useEffect Hook is the designated place for side effects. It runs after render, so the component's pure render logic is decoupled from impure effects.

Example

jsx
// Pure: just returns JSX from props
function Greeting({ name }) {
  return <h1>Hello, {name}</h1>;
}

// Side effect: fetches data after render
function UserProfile({ userId }) {
  const [user, setUser] = useState(null);
  useEffect(() => {
    fetch(`/api/users/${userId}`)
      .then(res => res.json())
      .then(setUser);
  }, [userId]);
  return user ? <h1>{user.name}</h1> : <Spinner />;
}

The fetch is a side effect — it reaches outside the component. useEffect runs it after render, keeping the render pure.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary