Skip to content

Optimize Marquee Animation with Focus Detection #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Animated, {
useSharedValue,
withDecay,
} from 'react-native-reanimated';
import {useFocusEffect} from '@react-navigation/native';


type MarqueeDirection = 'horizontal' | 'vertical';
export type MarqueeProps = React.PropsWithChildren<{
Expand Down Expand Up @@ -105,7 +107,7 @@ export const Marquee = React.memo(
});
const [cloneTimes, setCloneTimes] = React.useState(0);
const anim = useSharedValue(0);

const frameRateMs = frameRate ? 1000 / frameRate : null;

const frameCallback = useFrameCallback((frameInfo) => {
Expand Down Expand Up @@ -197,6 +199,18 @@ export const Marquee = React.memo(
);
});

// detect focus
useFocusEffect(
React.useCallback(() => {
frameCallback.setActive(true);

return () => {
frameCallback.setActive(false);
anim.value = 0;
};
}, []),
);

return (
<Animated.View
key={direction}
Expand Down