BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

© 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutSync devicesRSSGitHubX
Posts/react
AI-generated illustration. The list is the only thing worth keeping. The filtered view gets worked out on the spot.
Beginner5 min read

Part 2 of Common Mistakes

You Don't Need That useEffect: Derived State in React

If you can calculate a value from props or state, it isn't state. Copying it with useEffect costs an extra render and shows a stale value first.

Mohsen Fallahnejad

Mohsen Fallahnejad

Nov 21, 2026Discuss

A search box over a list. It works, and it shows up in code review every week:

jsx
function Results({ items, query }) {
  const [filtered, setFiltered] = useState(items);

  useEffect(() => {
    setFiltered(items.filter((item) => item.name.includes(query)));
  }, [items, query]);

  return <List items={filtered} />;
}

Type a letter and the list updates. Nothing looks broken. But every keystroke renders Results twice, and the first render shows the old list.

The mental model: a copy that runs late#

If you can calculate it from props or state, it isn't state. Calculate it while rendering.

filtered is a copy of an answer you already have. items and query are the source. A copy kept in its own state has to be updated after the source changes, and until then it's out of date.

AI-generated illustration. The copy on the left trails behind its source. The view on the right reads the source directly, so it can't fall behind.AI-generated illustration. The copy on the left trails behind its source. The view on the right reads the source directly, so it can't fall behind.

How the copy goes wrong#

Follow one keystroke through the code above:

  1. query changes, so React renders Results.
  2. That render still reads the old filtered, because the effect hasn't run yet. React puts the old list on screen.
  3. After the screen updates, the effect runs and calls setFiltered.
  4. That state change renders Results again, this time with the right list.

I counted this with React 19.3.1. Over 5,000 items, changing the query to "item 42" rendered the effect version twice. The first render still had all 5,000 items. The second had the right 111. Steps 2 and 3 are the useEffect-after-paint gap: a slow device can show the stale list for a frame.

The fix deletes code:

jsx
function Results({ items, query }) {
  const filtered = items.filter((item) => item.name.includes(query));
  return <List items={filtered} />;
}

Same test, one render, already showing 111 items. Nothing to keep in sync, because nothing was copied.

In practice#

The same pattern hides behind other names:

jsx
// before: state + effect
const [fullName, setFullName] = useState("");
useEffect(() => setFullName(`${first} ${last}`), [first, last]);

// after: a variable
const fullName = `${first} ${last}`;

Totals from a cart, isValid from form fields, selectedItem from a list plus an id: each one is a value you calculate while rendering, not a value you store.

If the calculation is genuinely slow, wrap it in useMemo. That still runs during the render, so there's no stale frame. It only skips the work when the inputs haven't changed:

jsx
const filtered = useMemo(
  () => items.filter((item) => item.name.includes(query)),
  [items, query],
);

Reach for an effect only when the code talks to something outside React: fetching, a subscription, a timer, the DOM. Copying one piece of React state into another isn't on that list.

Honest stakes: with a short list you won't see the stale frame, and the extra render costs little. The real cost is bugs. Two copies of one answer drift apart: someone updates items from a new place, forgets the effect's dependency list, and the screen disagrees with the data.

Say it in an interview#

"If a value can be computed from props or state, I compute it during render instead of storing it. Copying it into state with useEffect means the first render uses the old value, the effect runs after paint, and its setState causes a second render. Computing it inline gives one render with the right value. If the calculation is expensive I use useMemo. Effects are for syncing with things outside React, like the network or the DOM."

Cheat sheet#

You haveWrite
A value computed from props or stateA const in the component body
The same, but slow to computeuseMemo with its inputs
Data from the networkAn effect, or better, a data library
A value the user editsuseState

TL;DR#

  • If you can calculate it, it isn't state.
  • useState plus a useEffect that sets it gives two renders and a stale first frame.
  • Calculate the value in the component body. Use useMemo only when it's slow.
  • Effects are for the world outside React, not for copying state.

Advanced · optional

Going deeper#

Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.

How I counted the renders#

React 19.3.1 and react-dom 19.3.1, rendered with createRoot into a happy-dom document under Bun. Each component pushed a line to a log on every render. The list had 5,000 items named item 0 to item 4999. After mounting with an empty query, I re-rendered with query="item 42" inside act, which flushes effects:

VersionRenders after the changeWhat each render showed
useState + useEffect25,000 items, then 111
Computed in the body1111 items

Both versions also mounted the same way: two renders for the effect version, one for the computed one.

Resetting state when a prop changes#

A related case: state that the user does edit, but which should reset when an id changes, such as a comment draft per post. Don't reset it in an effect. Give the component a key:

jsx
<CommentBox key={postId} postId={postId} />

A new key makes React treat it as a new component, so its state starts fresh, in the same render. See why React needs key.

Further reading#

  • You Might Not Need an Effect, React docs
  • useEffect dependencies on this blog
Filed under#react#useeffect#state#hooks#interview

Series

Common Mistakes

Patterns that look fine in code review until production, the bundler, or an interview question proves otherwise.

  1. 1Barrel Files (`index.ts`): Why One Import Can Slow Your Build
  2. 2You Don't Need That useEffect: Derived State in React · you are here
  3. 3The forEach that did not wait for await · coming soon
  4. 4The secret that NEXT_PUBLIC_ copied into git · coming soon

New parts land here first — subscribe below to get them in your inbox.

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X ↗GitHub ↗LinkedIn ↗
← OlderBoolean(x) vs !!x, Number vs parseInt, and String() vs toString()

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

useEffect vs useLayoutEffect: Why the Tooltip Flashes, Then Jumps

useEffect runs after the browser paints. useLayoutEffect runs before that paint, so a measurement can fix the screen before anyone sees the wrong frame.

Nov 15, 20266 min read

React useEffect Dependencies — The Bug That Keeps Coming Back

Why your effect reads old state, fires twice, or loops forever — and the simple rules to fix it (interview-ready).

Jul 8, 20266 min read

Why React Needs key: The Bug That Moves Your Input to the Wrong Row

React matches list items between renders by key. Use the index and React gets the wrong match, so state ends up in the wrong row. How keys work and when an index is fine.

Nov 13, 20266 min read

Get new posts in your inbox

One email when a new post is out: short, diagram-first explanations of JavaScript, React and Next.js. No spam, unsubscribe any time.

5 min left
Discuss👍 React
👍