From 2309200aa8e7e4209a87abedfa6a82bd0d434d52 Mon Sep 17 00:00:00 2001 From: George Wizeman Date: Mon, 23 Sep 2024 17:53:01 +0700 Subject: [PATCH] fix: fixed hook deps in more explicit way --- src/AnimatedNumber.tsx | 21 ++++++++++------ src/Reel.tsx | 57 ++++++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/AnimatedNumber.tsx b/src/AnimatedNumber.tsx index 3d26a16..22f001c 100644 --- a/src/AnimatedNumber.tsx +++ b/src/AnimatedNumber.tsx @@ -1,8 +1,9 @@ -import { useMemo, useCallback } from "react"; -import { View, TextStyle } from "react-native"; +import { useMemo, useCallback } from 'react'; +import { View } from 'react-native'; +import type { TextStyle } from 'react-native'; -import { Reel } from "./Reel"; -import { styles, lineHeightMultiplier } from "./AnimatedNumberStyle"; +import { Reel } from './Reel'; +import { styles, lineHeightMultiplier } from './AnimatedNumberStyle'; type AnimatedNumberProps = { value: number; @@ -15,8 +16,14 @@ type AnimatedNumberProps = { function AnimatedNumber(props: AnimatedNumberProps) { const { value, format, fontSize, textStyle, duration } = props; - const formattedValue = useMemo(() => { return format ? format.format(value) : value.toString() }, [value, format]); - const symbols = useMemo(() => formattedValue.toString().split(""), [formattedValue]); + const formattedValue = useMemo(() => { + return format ? format.format(value) : value.toString(); + }, [value, format]); + + const symbols = useMemo( + () => formattedValue.toString().split(''), + [formattedValue] + ); const renderReels = useCallback(() => { return symbols.map((symbol, index) => { @@ -30,7 +37,7 @@ function AnimatedNumber(props: AnimatedNumberProps) { /> ); }); - }, [value, fontSize, duration]); + }, [symbols, fontSize, duration, textStyle]); const contentContainerStyle = useMemo( () => ({ diff --git a/src/Reel.tsx b/src/Reel.tsx index 9396efc..5c54f47 100644 --- a/src/Reel.tsx +++ b/src/Reel.tsx @@ -1,6 +1,11 @@ import { useMemo, useEffect, useCallback } from 'react'; -import { Text, FontVariant, TextStyle } from 'react-native'; -import Animated, { useSharedValue, useAnimatedStyle, withTiming } from 'react-native-reanimated'; +import { Text } from 'react-native'; +import Animated, { + useSharedValue, + useAnimatedStyle, + withTiming, +} from 'react-native-reanimated'; +import type { TextStyle, FontVariant } from 'react-native'; import { styles, lineHeightMultiplier } from './AnimatedNumberStyle'; @@ -9,17 +14,21 @@ type ReelProps = { textStyle?: TextStyle; symbol: string; duration: number; -} +}; const reelNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const frameDuration = 16; +const fontVariant = ['tabular-nums' as FontVariant]; function Reel(props: ReelProps) { const { fontSize, symbol, duration, textStyle } = props; - const fontVariant = ['tabular-nums' as FontVariant]; const lineHeight = fontSize * lineHeightMultiplier; - const fontStyle = useMemo(() => ({ ...textStyle, fontSize, lineHeight, fontVariant }), [textStyle, fontSize]); + + const fontStyle = useMemo( + () => ({ ...textStyle, fontSize, lineHeight, fontVariant }), + [textStyle, fontSize, lineHeight] + ); const translateY = useSharedValue(0); @@ -28,47 +37,45 @@ function Reel(props: ReelProps) { const style = useAnimatedStyle(() => { return { ...styles.reel, - transform: [{ translateY: translateY.value }] - } + transform: [{ translateY: translateY.value }], + }; }); useEffect(() => { if (isNumber) { const digit = parseInt(symbol, 10); - translateY.value = withTiming(fontSize * - lineHeightMultiplier * digit, { duration }); + translateY.value = withTiming(fontSize * -lineHeightMultiplier * digit, { + duration, + }); } else { // Direct setting to 0 from JS thread is async and slower than using withTiming translateY.value = withTiming(0, { duration: frameDuration }); } - }, [symbol]); - + }, [symbol, isNumber, duration, translateY, fontSize]); - const renderContent= useCallback(() => { + const renderContent = useCallback(() => { const renderReelNumbers = () => { return reelNumbers.map((number) => { return ( - {number} - ) + + {number} + + ); }); }; const renderSymbol = () => { return ( - {symbol} + + {symbol} + ); }; - return isNumber - ? renderReelNumbers() - : renderSymbol(); - - }, [fontSize, fontVariant, symbol]); + return isNumber ? renderReelNumbers() : renderSymbol(); + }, [fontStyle, isNumber, symbol]); - return ( - - {renderContent()} - - ) + return {renderContent()}; } -export { Reel }; \ No newline at end of file +export { Reel };