React Forum
Blog & Guide Responses
Aug 06, 2022 · 04:00 AM

You Might Not Need an Effect - discussion (2022-08-06)

Started by Jules Kwon
externalreact.deveffectsderived stateresetrender

I keep seeing "don't use effects for sync" in reviews and I agree in theory, but the migration path is where people get stuck.

Read the article

What are your go-to replacements for common sync effects (derived values, reset on prop change, fetch when X changes)? When do you keep an effect anyway because it's just simpler? Do you render signals for resets/derived computations so behavior is explainable from screenshots?

Comments (10)

Back to latest
Mina Calder
Aug 06, 2022 · 04:06 AM

The biggest win is deleting effects that compute derived state late. If it's derivable, derive it.

Dmitri Kline
Aug 06, 2022 · 04:14 AM

Resets are the key one: either do a keyed remount or make reset an explicit action, not timing.

If you hide reset in an effect, you get race conditions with back/forward.

Related: Core Concepts Best Practices and the routing angle in Navigation Routines (Deep Dive #4).

Salma Qureshi
Aug 06, 2022 · 04:22 AM

Keyed remount is blunt but deterministic:

tsx
function EditorRoute({ docId }: { docId: string }) {
  return <Editor key={docId} docId={docId} />;
}

The nice part is you can write a signal for the boundary: resetSignal={reason:'docId-change'} and tests can assert it without timing.

Hana Saito
Aug 06, 2022 · 04:31 AM

Counterpoint: keyed remounts can wipe user state in surprising ways (scroll, focus, unsaved drafts).

We prefer explicit reset actions when users might reasonably expect continuity.

Jules Kwon
Aug 06, 2022 · 04:36 AM

Agree. I treat remount as the hard boundary tool.

If users expect continuity, explicit reset actions + signals (and sometimes migration adapters) are safer.

Hana Saito
Aug 06, 2022 · 04:39 AM

Yep. The issue isn't remounts, it's using remounts as a hidden side effect.

Keira Santos
Aug 06, 2022 · 04:44 AM

For fetch-when-X-changes, we moved it into the render contract and rendered fetch signals. It removed the "why did it double fetch" class of issues.

Amina Farouk
Aug 06, 2022 · 04:52 AM

We still keep some effects because they're pragmatic (analytics, localStorage writes).

The rule is they must be idempotent and must not correct render output after the fact.

Maya Henson
Aug 06, 2022 · 05:03 AM

The strongest heuristic: if the effect exists to make render correct, it's a bug. If it exists because the outside world needs a side effect, it's probably fine.

Inez Park
Aug 06, 2022 · 05:15 AM

Signals are underrated here. A resetSignal turns "it cleared my input" into something you can reason about and test.

Benji Rios
Aug 06, 2022 · 05:27 AM

We also ban disabling the exhaustive-deps rule. If deps are annoying, it's usually telling you the design is unstable.

Noah Vance
Aug 06, 2022 · 05:41 AM

When you delete sync effects, you often end up with better URL contracts. It's a good forcing function.