v3 `usePanGesture`: `validateOffsetsArray` reads shared values during render (Reanimated warning in `__DEV__`)

未关闭 适合新手
#4,725 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, react-native, typescript
领域
frontend, mobile

调研方向

从 src/v3/hooks/gestures/pan/usePanGesture.ts 开始,重点查看 validateOffsetsArray 及其从 validatePanConfig 发起的调用;追踪 Shared Values 的类型定义,以及现有监听器如何传递它们。根据 issue 中的复现步骤确认开发环境警告,然后为 activeOffsetX 中的 Shared Values 添加或更新针对性测试。并验证普通数值 offset 仍能正确通过验证。

由索引模型根据 Issue 内容生成。

描述

Missing info Missing repro Platform: iOS

In __DEV__, usePanGesture calls validatePanConfig synchronously while rendering. validateOffsetsArray unpacks the array entries with maybeUnpackValue, which reads .value of a shared value. Reanimated then warns on every render:

[Reanimated] Reading from `value` during component render. Please ensure that you do not access the `value` property or use `get` method of a shared value while React is rendering a component.

Environment: react-native-gesture-handler 3.3.0 (code unchanged in 3.5.1), react-native-reanimated 4.7.1, Expo SDK 58, React Native 0.88.1, iOS 27.

Steps to reproduce:

const reach = useSharedValue(-40);
const pan = usePanGesture({ activeOffsetX: [reach, 20], onUpdate: () => {} });

Render the component in a dev build: the warning appears on every render (the types allow shared values inside these arrays via WithSharedValue).

Root cause: src/v3/hooks/gestures/pan/usePanGesture.ts:46-47 (maybeUnpackValue<number>(offsets[0]), called from validatePanConfig at line 57-60 and from the hook body at line 152).

Fix: validate only plain numbers; shared values are forwarded to the native side by the existing listeners anyway, so they are not validated at render time:

-  const offsetStart = maybeUnpackValue<number>(offsets[0]);
-  const offsetEnd = maybeUnpackValue<number>(offsets[1]);
-
-  if (offsetStart > 0 || offsetEnd < 0) {
+  const [offsetStart, offsetEnd] = offsets;
+
+  if (
+    (typeof offsetStart === 'number' && offsetStart > 0) ||
+    (typeof offsetEnd === 'number' && offsetEnd < 0)
+  ) {

The maybeUnpackValue import (line 11) then becomes unused. Happy to send this as a PR.

主要语言
TypeScript
星标
6.8k
派生
1.1k
平均合并
4 天 4 小时
30 天内合并 PR
22

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

software-mansion/react-native-gesture-handler 的其他 Issue

查看 software-mansion/react-native-gesture-handler 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。