gesture-handler-3-migration
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMigrate to Gesture Handler 3
迁移至Gesture Handler 3
This skill scans React Native components that use the Gesture Handler builder-based API and updates them to use the new hook-based API. It also updates related types and components to adapt to the new version.
本Skill会扫描使用Gesture Handler基于构建器API的React Native组件,并将其更新为使用新的基于Hook的API。同时还会更新相关类型与组件以适配新版本。
When to Use
使用场景
- Updating the usage of components imported from
react-native-gesture-handler - Upgrading to Gesture Handler 3
- Migrating to the new hook-based gesture API
- 更新从导入的组件用法
react-native-gesture-handler - 升级至Gesture Handler 3版本
- 迁移至新的基于Hook的手势API
Instructions
操作步骤
Use the instructions below to correctly replace all legacy APIs with the modern ones.
- Identify all imports from 'react-native-gesture-handler'
- For each call, replace with corresponding
Gesture.X()hookuseXGesture() - Replace import with imports for the used hooks
Gesture - Convert builder method chains to configuration objects
- Update callback names (onStart → onActivate, etc.)
- Replace composed gestures with relation hooks. Keep rules of hooks in mind
- Update GestureDetector usage if SVG is involved to Intercepting/Virtual GestureDetector
- Update usage of compoenent imported from 'react-native-gesture-handler' according to "Legacy components" section
请按照以下说明将所有旧版API正确替换为新版API。
- 识别所有从'react-native-gesture-handler'导入的内容
- 将每个调用替换为对应的
Gesture.X()HookuseXGesture() - 将导入替换为所使用Hook的导入
Gesture - 将构建器方法链转换为配置对象
- 更新回调名称(如onStart → onActivate等)
- 使用关联Hook替换组合手势,注意Hook的使用规则
- 若涉及SVG,将GestureDetector的用法更新为Intercepting/Virtual GestureDetector
- 根据「旧版组件」章节更新从'react-native-gesture-handler'导入的组件用法
Migrating gestures
迁移手势
All hook gestures have their counterparts in the builder API: becomes . The methods are now config object fields with the same name as the relevant builder methods, unless specified otherwise.
Gesture.X()useXGesture(config)The exception to thait is which DOES NOT have a counterpart in the hook API.
Gesture.ForceTouch所有Hook手势在构建器API中都有对应的版本:变为。方法现在是配置对象的字段,名称与相关构建器方法相同,除非另有说明。
Gesture.X()useXGesture(config)例外情况是,它在Hook API中没有对应版本。
Gesture.ForceTouchCallback changes
回调变更
In Gesture Handler 3 some of the callbacks were renamed, namely:
- ->
onStartonActivate - ->
onEndonDeactivate - ->
onTouchesCancelledonTouchesCancel
In the hooks API is no longer available. Instead the properties were moved to the event available inside .
onChange*change*onUpdateAll callbacks of a gesture are now using the same type:
- ->
usePanGesture()PanGestureEvent - ->
useTapGesture()TapGestureEvent - ->
useLongPressGesture()LongPressGestureEvent - ->
useRotationGesture()RotationGestureEvent - ->
usePinchGesture()PinchGestureEvent - ->
useFlingGesture()FlingGestureEvent - ->
useHoverGesture()HoverGestureEvent - ->
useNativeGesture()RotationGestureEvent - ->
useManualGesture()ManualGestureEvent
The exception to this is touch events:
onTouchesDownonTouchesUponTouchesMoveonTouchesCancel
Where each callback receives regardless of the hook used.
GestureTouchEvent在Gesture Handler 3中,部分回调名称被重命名,具体如下:
- ->
onStartonActivate - ->
onEndonDeactivate - ->
onTouchesCancelledonTouchesCancel
在Hook API中,不再可用。取而代之的是,属性被移至内部的事件中。
onChange*change*onUpdate手势的所有回调现在使用相同的类型:
- ->
usePanGesture()PanGestureEvent - ->
useTapGesture()TapGestureEvent - ->
useLongPressGesture()LongPressGestureEvent - ->
useRotationGesture()RotationGestureEvent - ->
usePinchGesture()PinchGestureEvent - ->
useFlingGesture()FlingGestureEvent - ->
useHoverGesture()HoverGestureEvent - ->
useNativeGesture()RotationGestureEvent - ->
useManualGesture()ManualGestureEvent
触摸事件是例外情况:
onTouchesDownonTouchesUponTouchesMoveonTouchesCancel
无论使用哪个Hook,这些回调都会接收类型。
GestureTouchEventStateManager
StateManager
In Gesture Handler 3, is no longer passed to callbacks. Instead, you should use the global .
stateManagerTouchEventGestureStateManagerGestureStateManager- .begin(handlerTag: number)
- .activate(handlerTag: number)
- .deactivate(handlerTag: number) (.end() in the old API)
- .fail(handlerTag: number)
handlerTag- From the gesture object returned by the hook ()
gesture.handlerTag - From the event inside callback ()
event.handlerTag
Callback definitions CANNOT reference the gesture that's being defined. In this scenario use events to get access to the handler tag.
在Gesture Handler 3中,不再传递给回调。取而代之的是,你应该使用全局的。
stateManagerTouchEventGestureStateManagerGestureStateManager- .begin(handlerTag: number)
- .activate(handlerTag: number)
- .deactivate(handlerTag: number)(旧版API中的.end())
- .fail(handlerTag: number)
handlerTag- 从Hook返回的手势对象中获取()
gesture.handlerTag - 从回调内部的事件中获取()
event.handlerTag
回调定义不能引用正在定义的手势。在这种情况下,请使用事件来访问handler tag。
Migrating relations
迁移关联关系
Composed gestures
组合手势
Gesture.Simultaneous(gesture1, gesture2);useSimultaneousGestures(pan1, pan2);All relations from the old API and their counterparts in the new one:
- ->
Gesture.Race()useCompetingGestures() - ->
Gesture.Simultaneous()useSimultaneousGestures() - ->
Gesture.Exclusive()useExclusiveGestures()
Gesture.Simultaneous(gesture1, gesture2);useSimultaneousGestures(pan1, pan2);旧版API中的所有关联关系及其在新版中的对应项:
- ->
Gesture.Race()useCompetingGestures() - ->
Gesture.Simultaneous()useSimultaneousGestures() - ->
Gesture.Exclusive()useExclusiveGestures()
Cross components relations properties
跨组件关联属性
Properties used to define cross-components interactions were renamed:
- ->
.simultaneousWithExternalGesturesimultaneousWith: - ->
.requireExternalGestureToFailrequireToFail: - ->
.blocksExternalGestureblock:
用于定义跨组件交互的属性被重命名:
- ->
.simultaneousWithExternalGesturesimultaneousWith: - ->
.requireExternalGestureToFailrequireToFail: - ->
.blocksExternalGestureblock:
GestureDetector
GestureDetector
The is a key component of . It supports gestures created either using the hooks API or the builder pattern (but those cannot be mixed, it's either or).
GestureDetectorreact-native-gesture-handlerDon't use the same instance of a gesture across multiple Gesture Detectors as it will lead to an undefined behavior.
GestureDetectorreact-native-gesture-handler不要在多个Gesture Detector中使用同一个手势实例,这会导致未定义行为。
Integration with Reanimated
与Reanimated集成
Worklets' Babel plugin is setup in a way that automatically marks callbacks passed to gestures in the configuration chain as worklets. This means that you don't need to add a directive at the beginning of the functions.
'worklet';This will not be workletized because the callback is defined outside of the gesture object:
jsx
const callback = () => {
console.log(_WORKLET);
};
const gesture = useTapGesture({
onBegin: callback,
});The callback wrapped by any other higher order function will not be workletized:
jsx
const gesture = useTapGesture({
onBegin: useCallback(() => {
console.log(_WORKLET);
}, []),
});In the above cases, you should add a directive as the first line of the callback.
"worklet";Worklets的Babel插件会自动将配置链中传递给手势的回调标记为worklet。这意味着你不需要在函数开头添加指令。
'worklet';以下情况不会被标记为worklet,因为回调是在手势对象外部定义的:
jsx
const callback = () => {
console.log(_WORKLET);
};
const gesture = useTapGesture({
onBegin: callback,
});被任何高阶函数包裹的回调也不会被标记为worklet:
jsx
const gesture = useTapGesture({
onBegin: useCallback(() => {
console.log(_WORKLET);
}, []),
});在上述情况下,你应该在回调的第一行添加指令。
"worklet";Disabling Reanimated
禁用Reanimated
Gestures created with the hook API have integration enabled by default (if it's installed), meaning all callbacks are executed on the UI thread.
Reanimated使用Hook API创建的手势默认启用Reanimated集成(如果已安装),这意味着所有回调都在UI线程上执行。
runOnJS
runOnJS
The property allows you to dynamically control whether callbacks are executed on the JS thread or the UI thread. When set to , callbacks will run on the JS thread. Setting it to will execute them on the UI thread. Default value is .
runOnJStruefalsefalserunOnJStruefalsefalseMigrating components relying on view hierarchy
迁移依赖视图层级的组件
Certain components, such as , depend on the view hierarchy to function correctly. In Gesture Handler 3, disrupts these hierarchies. To resolve this issue, two new detectors have been introduced: and .
SVGGestureDetectorInterceptingGestureDetectorVirtualGestureDetectorInterceptingGestureDetectorGestureDetectorVirtualGestureDetectorgestureVirtualGestureDetectorGestureDetectorWarning: has to be a descendant of .
VirtualGestureDetectorInterceptingGestureDetector某些组件(如)依赖视图层级才能正常工作。在Gesture Handler 3中,会破坏这些层级关系。为解决此问题,引入了两个新的检测器:和。
SVGGestureDetectorInterceptingGestureDetectorVirtualGestureDetectorInterceptingGestureDetectorGestureDetectorVirtualGestureDetectorgestureVirtualGestureDetectorGestureDetector警告: 必须是的后代组件。
VirtualGestureDetectorInterceptingGestureDetectorMigrating SVG
迁移SVG
In Gesture Handler 2 it was possible to use directly on . In Gesture Handler 3, the correct way to interact with is to use and .
GestureDetectorSVGSVGInterceptingGestureDetectorVirtualGestureDetector在Gesture Handler 2中,可以直接在上使用。在Gesture Handler 3中,与交互的正确方式是使用和。
SVGGestureDetectorSVGInterceptingGestureDetectorVirtualGestureDetectorLegacy components
旧版组件
When the code using the component relies on the APIs that are no longer available on the components in Gesture Handler 3 (like , , , , props), it cannot be easily migrated in isolation. In this case update the imports to the Legacy version of the component, and inform the user that the dependencies need to be migrated first.
waitForsimultaneousWithblocksHandleronHandlerStateChangeonGestureEventIf the migration is possible, use the ask questions tool to clarify the user intent unless clearly stated beforehand: should the components be using the new implementation (no prefix when imported), or should they revert to the old implementation ( prefix when imported)?
LegacyLegacyDon't suggest replacing buttons from Gesture Handler with components from React Native and vice versa.
The implementation of buttons has been updated, resolving most button-related issues. They have also been internally rewritten to utilize the new hook API. The legacy JS implementations of button components are still accessible but have been renamed with the prefix , e.g., is now available as . Those still use the new native component under the hood.
LegacyRectButtonLegacyRectButtonOther components have also been internally rewritten using the new hook API but are exported under their original names, so no changes are necessary on your part. However, if you need to use the previous implementation for any reason, the legacy components are also available and are prefixed with , e.g., is now available as .
LegacyScrollViewLegacyScrollView当使用组件的代码依赖Gesture Handler 3组件中不再可用的API(如、、、、属性)时,无法单独轻松迁移。在这种情况下,请将导入更新为组件的旧版(Legacy)版本,并告知用户需要先迁移依赖项。
waitForsimultaneousWithblocksHandleronHandlerStateChangeonGestureEvent如果可以迁移,请使用提问工具明确用户的意图(除非之前已明确说明):组件应使用新实现(导入时不带前缀),还是应恢复为旧实现(导入时带前缀)?
LegacyLegacy不要建议将Gesture Handler的按钮替换为React Native的组件,反之亦然。
按钮的实现已更新,解决了大多数与按钮相关的问题。它们也已内部重写为使用新的Hook API。按钮组件的旧版JS实现仍然可用,但已重命名为带前缀,例如现在可作为使用。这些组件仍然在底层使用新的原生组件。
LegacyRectButtonLegacyRectButton其他组件也已内部使用新的Hook API重写,但仍以原名称导出,因此你无需进行任何更改。不过,如果你因任何原因需要使用之前的实现,旧版组件也可用,且带有前缀,例如现在可作为使用。
LegacyScrollViewLegacyScrollViewReplaced types
已替换的类型
Most of the types used in the builder API, like , are still present in Gesture Handler 3. However, they are now used in new hook API. Types for builder API now have prefix, e.g. becomes .
TapGestureLegacyTapGestureLegacyTapGesture构建器API中使用的大多数类型(如)在Gesture Handler 3中仍然存在。但它们现在用于新的Hook API。构建器API的类型现在带有前缀,例如变为。
TapGestureLegacyTapGestureLegacyTapGesture