← all blogs

Most useEffect bugs are just missing derived state

avoid: state useEffect re-render better: items + query compute in render
One straight pass during render beats an extra effect bounce every time.

The problem: flickering UI and double renders

A huge share of confusing React bugs come from one very common habit: storing a value in useState, then writing a useEffect whose only job is to update that state whenever another prop or state variable changes.

It looks harmless when you write it. In fact, it feels intuitive if you are used to thinking step by step: “when query changes, update filteredItems.”

The problem is what React actually does on screen when query updates:

  1. React renders your component with the new query, but filteredItems still holds the old list.
  2. React paints that mismatched frame to the DOM.
  3. Now useEffect runs and calls setFilteredItems(...).
  4. React throws away the first pass and renders the entire tree a second time with the updated list.

On a small list you might not notice the extra pass. On a heavier screen with animations, scroll containers, or child effects, that one-frame mismatch shows up as jittery flickers, stale counts, and hard-to-trace race conditions.

With useEffect (2 renders + stale flash): Render 1 Flicker Effect Render 2 Derived in render (1 clean pass): Compute + Render 1 Paint fresh UI
Deriving values during render eliminates the extra commit and one-frame flicker.

The pattern to avoid

Here is the classic version you see in almost every codebase:

type Props = {
  items: { id: string; name: string }[];
  query: string;
};

export function ProductList({ items, query }: Props) {
  const [filtered, setFiltered] = useState(items);

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

  return (
    <ul>
      {filtered.map((item) => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

We now have two sources of truth (items + query, and filtered), a manual effect trying to glue them together, and twice as many renders on every keystroke.

The solution: derive it during render

Ask one simple question before creating a new useState: can I calculate this value from props or state I already have?

If the answer is yes, delete the useState and delete the useEffect. Compute it directly inside the component body:

export function ProductList({ items, query }: Props) {
  const q = query.trim().toLowerCase();
  const filtered = items.filter((item) => item.name.toLowerCase().includes(q));

  return (
    <ul>
      {filtered.map((item) => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

Now filtered can never go out of sync, there is zero stale frame, and React finishes the work in one clean render pass.

Only wrap that calculation in useMemo if the array is genuinely large (thousands of rows) and you actually measured a frame drop in the profiler. For ordinary lists, plain inline filtering is faster than the memoization overhead.

Another common trap: resetting state when an ID changes

The second place people reach for useEffect is resetting a form or draft message whenever the user selects a different item:

// Fragile: flashes the previous user's draft for one frame
function CommentBox({ postId }: { postId: string }) {
  const [draft, setDraft] = useState("");

  useEffect(() => {
    setDraft("");
  }, [postId]);

  return <textarea value={draft} onChange={(e) => setDraft(e.target.value)} />;
}

Instead of manually clearing every piece of local state in an effect, pass postId as a React key from the parent:

// Clean: React automatically mounts a fresh instance with clean state
<CommentBox key={postId} postId={postId} />

When key changes, React unmounts the old CommentBox and mounts a brand new one. Every single state hook inside it resets cleanly on the first render with zero stale flash.

When useEffect actually IS the right tool

Avoiding unnecessary effects does not mean useEffect is bad. It just has a very specific job: synchronizing React with systems outside React.

Good reasons to use useEffect:

  • Subscribing to browser events (resize, keydown, online) and cleaning them up
  • Starting and clearing timers or IntersectionObserver instances
  • Connecting to WebSockets or third-party non-React widgets (maps, charts, video players)

If your effect is only moving data from one React variable into another React variable, you almost certainly want derived state or an event handler instead.