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

Đang mở Phù hợp với người mới
#4,725 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
85/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
react, react-native, typescript
Lĩnh vực
frontend, mobile

Hướng nghiên cứu

Bắt đầu từ src/v3/hooks/gestures/pan/usePanGesture.ts, đặc biệt là validateOffsetsArray và lời gọi hàm này từ validatePanConfig; theo dõi cách Shared Values được định kiểu và chuyển tiếp bởi các listener hiện có. Xác nhận cảnh báo trong môi trường phát triển bằng cách tái hiện lỗi trong issue, sau đó thêm hoặc cập nhật một bài kiểm thử tập trung cho Shared Values trong activeOffsetX. Đồng thời xác minh rằng các offset số thông thường vẫn được xác thực chính xác.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
6.8k
Fork
1.1k
Merge trung bình
4 ngày 4 giờ
Pull request đã merge (30 ngày)
22

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của software-mansion/react-native-gesture-handler

Tất cả issue của software-mansion/react-native-gesture-handler

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.