Last active
May 20, 2023 01:53
-
-
Save mmazzarolo/62697ec43b85b8cda1d28331ab49b9cc to your computer and use it in GitHub Desktop.
A basic "useAnimation" hook for React-Native animations
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
import { useRef } from "react"; | |
import { Animated, Easing } from "react-native"; | |
export const useAnimation = function(initialValue: number = 0) { | |
const endValue = initialValue === 0 ? 1 : 0; | |
const animationValueRef = useRef<Animated.Value>(new Animated.Value(initialValue)); | |
const setup = (config: Partial<Animated.TimingAnimationConfig> = {}) => | |
Animated.timing(animationValueRef.current, { | |
toValue: endValue, | |
useNativeDriver: true, | |
easing: Easing.inOut(Easing.quad), | |
...config | |
}); | |
return { | |
value: animationValueRef.current, | |
setup: setup | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: