Created
December 27, 2019 10:38
-
-
Save terrysahaidak/15822c4bf5c5c73b1308fd8e84fa7071 to your computer and use it in GitHub Desktop.
Hook for reanimated animated value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function useAnimatedValue( | |
value: number, | |
listener?: boolean | ((arg: any) => any), | |
debugLabel?: string, | |
) { | |
const lastValue = useRef(null); | |
const animatedValue = useMemo(() => new Animated.Value(value), []); | |
useEffect(() => { | |
if (!listener) { | |
return; | |
} | |
const animatedAlways = Animated.always( | |
Animated.call([animatedValue], ([val]) => { | |
lastValue.current = val; | |
if (typeof listener === 'function') { | |
listener(val); | |
} | |
if (debugLabel) { | |
console.log(debugLabel, val); | |
} | |
}), | |
); | |
animatedAlways.__attach(); | |
// return undo function | |
return () => animatedAlways.__detach(); | |
}, []); | |
animatedValue.getValue = () => lastValue.current; | |
return animatedValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment