Skip to content

Instantly share code, notes, and snippets.

@ivanpagac
Created March 6, 2020 20:21
Show Gist options
  • Save ivanpagac/06476974dbdd82d6ce322f22080700ec to your computer and use it in GitHub Desktop.
Save ivanpagac/06476974dbdd82d6ce322f22080700ec to your computer and use it in GitHub Desktop.
export const useAnimateOpacityOnValueChange = (
valueToWatchForChange: boolean,
trueOpacityValue: number = 1,
falseOpacityValue: number = 0.4
) => {
const calculateOpacity = (val: boolean) =>
valueToWatchForChange ? trueOpacityValue : falseOpacityValue;
const opacityAnimatedValue = useRef(
new Animated.Value(calculateOpacity(valueToWatchForChange))
).current;
useEffect(() => {
Animated.timing(opacityAnimatedValue, {
toValue: calculateOpacity(valueToWatchForChange),
duration: 300,
useNativeDriver: true
}).start();
}, [valueToWatchForChange]);
return [opacityAnimatedValue];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment