Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
yarn.lock
17 changes: 13 additions & 4 deletions components/ViewPortDetector.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useContext, useEffect, useRef } from "react";
import { Dimensions, View, ViewStyle } from "react-native";
import { Dimensions, Platform, View, ViewStyle } from "react-native";
import { LayoutRectangle } from "../types";
import { checkInViewPort } from "../utils";
import { ViewPortDetectorContext } from "./ViewPortDetectorContext";
Expand Down Expand Up @@ -51,6 +51,8 @@ export const ViewPortDetector: React.FC<Props> = ({
...props
}) => {
const view = useRef<View>(null);
const lastInViewPortValue = useRef(false);

const { parentLayout } = useContext(ViewPortDetectorContext);
const parentLayoutRef = useRef<LayoutRectangle>({
x: 0,
Expand Down Expand Up @@ -104,17 +106,24 @@ export const ViewPortDetector: React.FC<Props> = ({
if (newValue && runOnce) {
clearInterval(interval);
}
onChange(newValue);

// Only call onChange callback when inViewPort value changed
if (newValue !== lastInViewPortValue.current) {
onChange(newValue)
lastInViewPortValue.current = newValue
}
}
);
}, frequency);
return () => {
clearInterval(interval);
};
}, []);
}, [onChange]);

return (
<View ref={view} {...props}>
// Having collapsable={false} will disable view flattening on Android which is a workaronud to fix the issue with measure() on android
// https://github.com/facebook/react-native/issues/29712
<View ref={view} collapsable={Platform.OS !== 'android'} {...props}>
{children}
</View>
);
Expand Down