useAnimatedGestureHandler onStart 方法正确的上下文参数类型
useAnimatedGestureHandler onStart method correct context argument type
长话短说;博士
有没有人举例说明正确的泛型是 useAnimatedGestureHandler?
问题:
我正在关注 this 关于 Reanimated 2 手势动画的教程。有这个例子:
//...
const onGestureEvent =
useAnimatedGestureHandler({
onStart: (_, ctx) => {
ctx.offsetX = translateX.value
},
//...
})
//...
我使用打字稿,当我在打字稿中复制示例时,我得到 ctx
参数(上下文)类型错误:
Property 'offset' does not exist on type '{}'.
在 onStart 声明中进行了一些窥探之后,我发现 GestureHandlers 的完整类型需要一个泛型 <T, TContext extends Context>
:
//...
export interface GestureHandlers<T, TContext extends Context> {
onStart?: Handler<T, TContext>;
//...
}
解决方法:
我能够通过简单地传递实用程序类型 Record(这几乎与说 'any' 相同)来解决这个问题,我不喜欢这样。
const onGestureEvent = useAnimatedGestureHandler<
GestureEvent<PanGestureHandlerEventPayload>,
Record<string, unknown>
>({
onStart: (_, ctx) => {
ctx.offsetX = translateX.value;
},
// onActive: () => {},
// onEnd: () => {},
});
问题:
有没有人举例说明 useAnimatedGestureHandler 的正确泛型是什么?
According to the documentation,context
是“一个普通的JS对象,可以用来存储一些状态”,“你可以读取和写入任何数据”。您只需要自己定义状态的接口。
源存储库有一个 Example project that demonstrates this. For instance, here and here 是两个例子,它们定义了一个 AnimatedGHContext
类型,它们作为通用传递。
因此在您的情况下,您将定义并传递如下类型:
type AnimatedGHContext = {
offsetX: number;
};
长话短说;博士 有没有人举例说明正确的泛型是 useAnimatedGestureHandler?
问题:
我正在关注 this 关于 Reanimated 2 手势动画的教程。有这个例子:
//...
const onGestureEvent =
useAnimatedGestureHandler({
onStart: (_, ctx) => {
ctx.offsetX = translateX.value
},
//...
})
//...
我使用打字稿,当我在打字稿中复制示例时,我得到 ctx
参数(上下文)类型错误:
Property 'offset' does not exist on type '{}'.
在 onStart 声明中进行了一些窥探之后,我发现 GestureHandlers 的完整类型需要一个泛型 <T, TContext extends Context>
:
//...
export interface GestureHandlers<T, TContext extends Context> {
onStart?: Handler<T, TContext>;
//...
}
解决方法:
我能够通过简单地传递实用程序类型 Record(这几乎与说 'any' 相同)来解决这个问题,我不喜欢这样。
const onGestureEvent = useAnimatedGestureHandler<
GestureEvent<PanGestureHandlerEventPayload>,
Record<string, unknown>
>({
onStart: (_, ctx) => {
ctx.offsetX = translateX.value;
},
// onActive: () => {},
// onEnd: () => {},
});
问题:
有没有人举例说明 useAnimatedGestureHandler 的正确泛型是什么?
According to the documentation,context
是“一个普通的JS对象,可以用来存储一些状态”,“你可以读取和写入任何数据”。您只需要自己定义状态的接口。
源存储库有一个 Example project that demonstrates this. For instance, here and here 是两个例子,它们定义了一个 AnimatedGHContext
类型,它们作为通用传递。
因此在您的情况下,您将定义并传递如下类型:
type AnimatedGHContext = {
offsetX: number;
};