Skip to content
Merged
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions src/reanimated2/hook/useScrollViewOffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ const IS_WEB = isWeb();
* @see https://docs.swmansion.com/react-native-reanimated/docs/scroll/useScrollViewOffset
*/
export const useScrollViewOffset = IS_WEB
? useScrollViewOffsetJS
? useScrollViewOffsetWeb
: useScrollViewOffsetNative;

function useScrollViewOffsetJS(
function useScrollViewOffsetWeb(
animatedRef: AnimatedRef<AnimatedScrollView>,
initialRef?: SharedValue<number>
): SharedValue<number> {
Expand Down Expand Up @@ -85,6 +85,7 @@ function useScrollViewOffsetNative(
// eslint-disable-next-line react-hooks/rules-of-hooks
initialRef !== undefined ? initialRef : useSharedValue(0)
);
const scrollRef = useRef<AnimatedScrollView | null>(null);

const eventHandler = useEvent<RNNativeScrollEvent>(
(event: ReanimatedScrollEvent) => {
Expand All @@ -100,11 +101,15 @@ function useScrollViewOffsetNative(
) as unknown as EventHandlerInternal<ReanimatedScrollEvent>;

useEffect(() => {
const component = animatedRef.current;
const viewTag = IS_WEB ? component : findNodeHandle(component);
// We need to make sure that listener for old animatedRef value is removed
if (scrollRef.current !== null) {
eventHandler.workletEventHandler?.unregisterFromEvents();
Comment thread
szydlovsky marked this conversation as resolved.
Outdated
}
scrollRef.current = animatedRef.current;

const component = animatedRef.current;
const viewTag = findNodeHandle(component);
eventHandler.workletEventHandler.registerForEvents(viewTag as number);

return () => {
eventHandler.workletEventHandler?.unregisterFromEvents();
};
Expand Down