Skip to content

Commit

Permalink
Merge pull request #13 from ivanstnsk-ecto/master
Browse files Browse the repository at this point in the history
Fix issue#12
  • Loading branch information
SvanBoxel authored Dec 8, 2022
2 parents 3620d6c + b81fe78 commit 7f28561
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState, useRef } from 'react';
import React, { useEffect, useState, useRef, ReactNode, FC } from 'react';
import { View, Dimensions } from 'react-native';

export interface IDimensionData {
Expand All @@ -11,10 +11,15 @@ export interface Props {
/** Function that is triggered when component enters the viewport */
onChange(visible: boolean): any;
/** The component that needs to be in the viewport */
children: React.ReactNode;
children: ReactNode;
}

const VisibilitySensor: React.FC<Props> = (props) => {
const RNView = View as any

const VisibilitySensor: FC<Props> = ({
children,
onChange,
}) => {
const myView: any = useRef(null);
const [lastValue, setLastValue] = useState<boolean>(false);
const [dimensions, setDimensions] = useState<IDimensionData>({
Expand Down Expand Up @@ -69,17 +74,17 @@ const VisibilitySensor: React.FC<Props> = (props) => {

if (lastValue !== isVisible) {
setLastValue(isVisible);
props.onChange(isVisible);
onChange(isVisible);
} else {
props.onChange(isVisible);
onChange(isVisible);
}
};

return (
<View collapsable={false} ref={myView} {...props}>
{props.children}
<RNView collapsable={false} ref={myView}>
{children}
<View />
</View>
</RNView>
);
};

Expand Down

0 comments on commit 7f28561

Please sign in to comment.