Skip to content

Instantly share code, notes, and snippets.

@terrysahaidak
Created December 27, 2019 10:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save terrysahaidak/15822c4bf5c5c73b1308fd8e84fa7071 to your computer and use it in GitHub Desktop.
Save terrysahaidak/15822c4bf5c5c73b1308fd8e84fa7071 to your computer and use it in GitHub Desktop.
Hook for reanimated animated value
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