Section 04 · 17 rules

Side effects & errors

Side effects

  • SE1 MUST use useEffect only to sync with an external system: a native module, a subscription, a timer, a device API. If nothing external is involved, there is no Effect.
  • SE2 MUST put logic caused by a user action (submit, analytics on tap, navigate after success) in the event handler or the mutation’s onSuccess. NEVER in an Effect that watches state.
  • SE3 NEVER fetch data in useEffect; server data goes through the query library (ST1). Where a raw fetch is unavoidable, its cleanup MUST ignore stale responses.
  • SE4 MUST return a cleanup for every listener, subscription and timer (AppState, Keyboard, NetInfo, setInterval).
  • SE5 NEVER chain Effects that set state. Compute derived values in render and set all next state in one event handler.
  • SE6 MUST reset a component’s state on a prop change with a key, not with an Effect.
  • SE7 MUST run app-wide setup (SDK init, crash reporting, analytics) once at the app entry, never inside a screen.
  • SE8 MUST use the navigator’s focus hook (useFocusEffect) for work tied to a screen being visible (in Expo Router projects, import it from expo-router, NV2). Screens in stacks and tabs stay mounted, so useEffect fires at the wrong time.
  • SE9 NEVER disable react-hooks/exhaustive-deps or the React Compiler lint rules. A dependency loop means the logic belongs elsewhere (SE1, SE2).

Error boundaries and error handling

  • EB1 MUST have one root error boundary that shows a recoverable fallback (retry + a way to get help) and reports the error. Boundaries are still class components in React 19: use the router’s built-in boundary or react-error-boundary (vetted per V1–V8), never a hand-written class per feature.
  • EB2 MUST give every screen its own boundary, so one broken screen keeps the tab bar and header alive. Expo Router: export ErrorBoundary from the route file, or set a screen error boundary on the layout. React Navigation: wrap the screen component. Check the installed version’s docs first (A5); some of these APIs are marked unstable.
  • EB3 SHOULD add a boundary around a single widget only when that widget is optional (promo banner, chart). NEVER wrap every component.
  • EB4 MUST handle errors boundaries do not catch: event handlers and async code. Use try/catch, the mutation’s onError, or hand the error to the nearest boundary (showBoundary in react-error-boundary).
  • EB5 MUST render all four states for every query: loading, error with a retry, empty, success. NEVER a blank screen or an endless spinner.
  • EB6 MUST report errors once, centrally: the boundary’s onError plus a global JS error handler, sent to the crash reporter the repo already uses. NEVER swallow an error (catch {}) or only console.log it.
  • EB7 NEVER show raw error messages or stack traces to users; map errors to plain copy and keep the detail in the report. NEVER include tokens or personal data in reports.
  • EB8 MUST make the fallback’s retry reset the failed part (retry, resetErrorBoundary, or resetKeys tied to the route params).
// app/transfer/[id].tsx (Expo Router): a per-screen boundary + all four query states
export function ErrorBoundary({ error, retry }: ErrorBoundaryProps) {
  report(error); // central reporter, no PII
  return <ErrorState message="Couldn't load this transfer" onRetry={retry} />;
}

export default function TransferScreen() {
  const { id } = useLocalSearchParams<{ id: string }>();
  const q = useQuery(transferQueries.detail(id));
  if (q.isPending) return <LoadingState />;
  if (q.isError) return <ErrorState onRetry={q.refetch} />;
  if (!q.data) return <EmptyState />;
  return <TransferDetail transfer={q.data} />;
}