Skip to content

Commit

Permalink
chore: derive end reached value instead of reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhaup committed May 11, 2024
1 parent 9eba8ed commit 57f085f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/components/LazyChild.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback } from 'react';
import Animated, {
measure,
runOnJS,
Expand All @@ -24,12 +24,12 @@ export function LazyChild({
const _viewRef = useAnimatedRef<Animated.View>();
const hasFiredTrigger = useSharedValue(false);

const handleTrigger = () => {
const handleTrigger = useCallback(() => {
if (!hasFiredTrigger.value) {
hasFiredTrigger.value = true;
onThresholdPass();
}
};
}, [hasFiredTrigger, onThresholdPass]);

useAnimatedReaction(
() => {
Expand Down
52 changes: 24 additions & 28 deletions src/components/LazyScrollView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useRef } from 'react';
import React, { useCallback, useRef } from 'react';
import { View, type LayoutChangeEvent } from 'react-native';
import Animated, {
useAnimatedReaction,
useAnimatedRef,
useDerivedValue,
useScrollViewOffset,
Expand Down Expand Up @@ -39,39 +38,36 @@ export function LazyScrollView({
* Starts at 0 and increases as the user scrolls down
*/
const scrollValue = useScrollViewOffset(_scrollRef);
const hasReachedEnd = useSharedValue(false);
const hasReachedEnd = useDerivedValue(() => {
if (!_contentHeight.value || !_containerHeight.value) {
return false;
}

return scrollValue.value >= _contentHeight.value - _containerHeight.value;
});
const triggerValue = useDerivedValue(
() => _containerHeight.value + _offset.value
);

useAnimatedReaction(
() => {
if (!_contentHeight.value || !_containerHeight.value) {
return false;
}

return scrollValue.value >= _contentHeight.value - _containerHeight.value;
const onLayout = useCallback(
(e: LayoutChangeEvent) => {
_containerHeight.value = e.nativeEvent.layout.height;
_wrapperRef.current?.measureInWindow(
(_: number, y: number, _2: number, height: number) => {
_scrollViewTopY.value = y;
_contentHeight.value = height;
}
);
},
(reachedEnd) => {
if (reachedEnd && !hasReachedEnd.value) {
hasReachedEnd.value = true;
}
}
[_containerHeight, _contentHeight, _scrollViewTopY]
);

const onLayout = (e: LayoutChangeEvent) => {
_containerHeight.value = e.nativeEvent.layout.height;
_wrapperRef.current?.measureInWindow(
(_: number, y: number, _2: number, height: number) => {
_scrollViewTopY.value = y;
_contentHeight.value = height;
}
);
};

const onContentContainerLayout = (e: LayoutChangeEvent) => {
_contentHeight.value = e.nativeEvent.layout.height;
};
const onContentContainerLayout = useCallback(
(e: LayoutChangeEvent) => {
_contentHeight.value = e.nativeEvent.layout.height;
},
[_contentHeight]
);

return (
<Animated.ScrollView
Expand Down

0 comments on commit 57f085f

Please sign in to comment.