I try to animate a view to hide and show it. the duration is ok to open it (500), but it doesn't work when I close it, the duration is not respected (it closes directly).
Here is my code:
const {height, width} = Dimensions.get('window');
const [initialHeight] = useState(new Animated.Value(0));
useEffect(() => {
if (state === true) {
Animated.timing(initialHeight, {
toValue: height - 400,
duration: 500,
}).start();
} else {
Animated.timing(initialHeight, {
toValue: 0,
duration: 500,
}).start();
}
}, [height, initialHeight, state]);
...
<Animated.View style={{height: initialHeight, paddingVertical: 12}}>
What am I missing?
---Edit
I made this change, but it didn't solve the problem:
const [initialHeight, setInitialHeight] = useState(new Animated.Value(0));
useEffect(() => {
if (state === true) {
Animated.timing(initialHeight, {
toValue: height - 400,
duration: 500,
}).start(() => {
setInitialHeight(new Animated.Value(height - 400));
});
} else {
Animated.timing(initialHeight, {
toValue: 0,
duration: 500,
}).start(() => {
setInitialHeight(new Animated.Value(0));
});
}
}, [height, initialHeight, state]);