Skip to content

[General] Don't use context from useHandler#4143

Open
j-piasecki wants to merge 2 commits intomainfrom
@jpiasecki/dont-use-use-handler-context
Open

[General] Don't use context from useHandler#4143
j-piasecki wants to merge 2 commits intomainfrom
@jpiasecki/dont-use-use-handler-context

Conversation

@j-piasecki
Copy link
Copy Markdown
Member

Description

Instead of using context from useHandler to store data about the last received event, this PR introduces a global mutable object that holds a map in the UI runtime, allowing data to be stored by handler tags. This, along with the changes introduced in software-mansion/react-native-reanimated#9334, should significantly reduce the overhead of using gestures with Reanimated.

Test plan

Tested on Touchable stress test but added a Pan gesture. With this and the Reanimated change, the average dropped from 933ms to 810ms.

Copilot AI review requested due to automatic review settings May 4, 2026 10:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR replaces the per-handler useHandler context usage in the RNGH3 hook callback path with a shared mutable cache keyed by handlerTag, aiming to reduce Reanimated overhead for gesture updates.

Changes:

  • Added a module-level mutable map in useReanimatedEventHandler to store lastUpdateEvent state by handler tag.
  • Relaxed eventHandler update-path context handling so it no longer throws when context is absent.
  • Extended the local Reanimated wrapper typings with makeMutable.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
packages/react-native-gesture-handler/src/v3/hooks/callbacks/useReanimatedEventHandler.ts Replaces per-hook Reanimated context usage with a shared mutable handler-tag map in the UI runtime.
packages/react-native-gesture-handler/src/v3/hooks/callbacks/eventHandler.ts Makes update/finalize logic tolerate missing context instead of throwing.
packages/react-native-gesture-handler/src/handlers/gestures/reanimatedWrapper.ts Adds makeMutable to the local Reanimated type surface.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +27 to +29
const lastUpdateEventMap = Reanimated?.makeMutable(
new Map<number, ReanimatedContext<unknown>>()
);
Co-authored-by: Copilot <copilot@github.com>
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.

2 participants