diff --git a/src/reanimated2/NativeMethods.ts b/src/reanimated2/NativeMethods.ts index 66613324d74..bb4d8f88a2e 100644 --- a/src/reanimated2/NativeMethods.ts +++ b/src/reanimated2/NativeMethods.ts @@ -26,11 +26,11 @@ export function measure( if (!_WORKLET) { console.warn( - '[Reanimated] measure() was called from the main JS context. Measure is' + - 'only available in the UI runtime. This may also happen if measure()' + - 'was called by a worklet in the useAnimatedStyle hook, because useAnimatedStyle' + - 'calls the given worklet on the JS runtime during render. If you want to' + - 'prevent this warning then wrap the call with `if (_WORKLET)`. Then it will' + + '[Reanimated] measure() was called from the main JS context. Measure is ' + + 'only available in the UI runtime. This may also happen if measure() ' + + 'was called by a worklet in the useAnimatedStyle hook, because useAnimatedStyle ' + + 'calls the given worklet on the JS runtime during render. If you want to ' + + 'prevent this warning then wrap the call with `if (_WORKLET)`. Then it will ' + 'only be called on the UI runtime after the render has been completed.' ); return null; @@ -45,7 +45,12 @@ export function measure( } const measured = _measure(viewTag); - if (measured.x === -1234567) { + if (measured === null) { + console.warn( + `[Reanimated] The view with tag ${viewTag} has some undefined, not-yet-computed or meaningless value of \`LayoutMetrics\` type. This may be because the view is not currently rendered, which may not be a bug (e.g. an off-screen FlatList item).` + ); + return null; + } else if (measured.x === -1234567) { console.warn( `[Reanimated] The view with tag ${viewTag} returned an invalid measurement response` ); diff --git a/src/reanimated2/globals.d.ts b/src/reanimated2/globals.d.ts index 8acba83f658..3b803714a2a 100644 --- a/src/reanimated2/globals.d.ts +++ b/src/reanimated2/globals.d.ts @@ -20,7 +20,7 @@ declare global { name: string, updates: StyleProps | AnimatedStyle ) => void; - const _measure: (viewTag: number) => MeasuredDimensions; + const _measure: (viewTag: number) => MeasuredDimensions | null; const _scrollTo: ( viewTag: number, x: number, @@ -40,7 +40,7 @@ declare global { _WORKLET: boolean; __reanimatedModuleProxy: NativeReanimated; _frameTimestamp: number | null; - _measure: () => MeasuredDimensions; + _measure: () => MeasuredDimensions | null; _scrollTo: () => void; _chronoNow: () => number; performance: { now: () => number };