Skip to content

Commit

Permalink
fix: fix memory leak
Browse files Browse the repository at this point in the history
fix #137
  • Loading branch information
dohooo committed Feb 28, 2022
1 parent 94c0f89 commit 73a5fe2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/hooks/useCheckMounted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';

export function useCheckMounted() {
const mounted = React.useRef(false);

React.useEffect(() => {
mounted.current = true;
return () => {
mounted.current = false;
};
}, []);

return mounted;
}
11 changes: 7 additions & 4 deletions src/layouts/BaseLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Animated, {
useAnimatedStyle,
useDerivedValue,
} from 'react-native-reanimated';
import { useCheckMounted } from 'src/hooks/useCheckMounted';
import { IOpts, useOffsetX } from '../hooks/useOffsetX';
import type { IVisibleRanges } from '../hooks/useVisibleRanges';
import { LazyView } from '../LazyView';
Expand All @@ -24,6 +25,7 @@ export const BaseLayout: React.FC<{
animationValue: Animated.SharedValue<number>;
}) => React.ReactElement;
}> = (props) => {
const mounted = useCheckMounted();
const { handlerOffsetX, index, children, visibleRanges, animationStyle } =
props;

Expand Down Expand Up @@ -86,10 +88,11 @@ export const BaseLayout: React.FC<{
useAnimatedReaction(
() => visibleRanges.value,
() => {
runOnJS(updateView)(
visibleRanges.value.negativeRange,
visibleRanges.value.positiveRange
);
mounted.current &&
runOnJS(updateView)(
visibleRanges.value.negativeRange,
visibleRanges.value.positiveRange
);
},
[visibleRanges.value]
);
Expand Down

0 comments on commit 73a5fe2

Please sign in to comment.