Home / Learn React / You Might Not Need an Effect

You Might Not Need an Effect

Effects are an escape hatch. A lot of beginner code uses them where plain render logic or an event handler is simpler and faster. Learn the common cases.

In shortIf you can calculate it from props and state, do it during render. If it happens because of a click, do it in the handler.

Theory in plain words

Where does the code belong?Render or handlerderived valuesresponding to clicksresetting state with a keysending a formuseEffectfetching on mountsubscriptionstimerssyncing with outside systems
Where does the code belong?

What it is

This lesson is a checklist for removing unnecessary effects. The question to ask is: why does this code run? Because the component appeared on screen (effect) or because the user did something (event handler)?

Why we use it

Extra effects cause extra renders, flickers, infinite loops and bugs where values are out of sync. Removing them makes code shorter and more reliable.

How it works

Anything computed from existing props or state belongs in the render body. Anything triggered by a user action belongs in a handler. Effects are for syncing with external systems.

Where it is used

  • Code reviews of React projects
  • Fixing "state is one render behind" bugs
  • Cleaning up filtered lists and derived values

Case 1: derived values

Wrong vs rightTry it Yourself

The wrong version renders twice for every change. The right version renders once.

Case 2: reset state when a prop changes

Use a key to resetTry it Yourself

Changing key makes React throw the old component away and start a fresh one with empty state. No effect needed.

Case 3: user actions go in handlers

Do the work in the clickTry it Yourself

Case 4: expensive calculation

If a calculation is truly slow, wrap it in useMemo (next lessons) instead of an effect that stores the result in state.

Quick quiz

1. Filtered list based on props and state. Where should it be calculated?

2. How can you reset a component state when a prop like userId changes, without an effect?

3. Where should code that runs because the user clicked Save go?

Common questions

Filtered list based on props and state. Where should it be calculated?

During render. Derived values should be calculated during rendering.

How can you reset a component state when a prop like userId changes, without an effect?

Give it key={userId}. A new key makes React create a fresh instance.

Where should code that runs because the user clicked Save go?

The event handler. Actions caused by events belong in handlers.