Skip to content

fix(presets): preset hooks no longer catch React's hook errors - #964

Draft
danfry1 wants to merge 4 commits into
mainfrom
747-preset-hooks-surface-errors
Draft

danfry1 wants to merge 4 commits into
mainfrom
747-preset-hooks-surface-errors

Conversation

@danfry1

@danfry1 danfry1 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

The navigation preset's useNavigation(), useRoute() and useTheme() throw outside a render, as React Navigation's own hooks do, instead of returning a default. Reanimated's corrected hooks still answer outside a render, as Reanimated's own mock does, but no longer catch any error from React. Closes #747.

Why

The catches also hid hook errors inside a render. With a second copy of React, useTheme() returned DefaultTheme, and a test asserting a themed colour failed with "Unable to find an element", far from the cause.

Changes

  • The three navigation hooks no longer catch. useNavigationBuilder().describe() reuses the navigation its render read, rather than calling a hook after the render.
  • Reanimated's useRefOrNull recognises "outside a render" from React's dispatcher: null before any render, or the context-only one React leaves between renders. Otherwise it calls useRef with no try/catch.
  • The structural scorecard's swallowed-catch count drops from 58 to 56.

Notes

The navigation preset's useNavigation(), useRoute() and useTheme() throw outside a
render, as React Navigation's own hooks do, instead of returning a default that also
hid hook errors inside a render. Reanimated's corrected hooks still answer outside a
render, as Reanimated's own mock does, but now recognise it from React's dispatcher
(null, or the context-only one left between renders) and catch nothing.

Carries #757's changes onto main. Fixes #747.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preset hooks catch React's hook errors and return default values

1 participant