I'm quite a new using React Native and I'm trying to display a component only if the internet connection is not reachable. When it is the component must disappear.
My problem is that this component must not be shown on the startup. If the connection is false, the component must be animate from out of the screen to his wanted position. Then if the connection state change to true, it must disappear to his initial position out of the screen.
I'm able to do that but it's always shown on startup.
Here is my code structure :
const HealthCheckStatus = (): Node => {
const [status, setStatus] = useState(true);
const netInfo = useNetInfo();
useEffect(() => {
if (netInfo.isInternetReachable != null) {
setStatus(netInfo.isInternetReachable);
}
}, [netInfo.isInternetReachable]);
const animatedStyles = useAnimatedStyle(
() => ({
transform: [
{
translateY: withSequence(withSpring(-100), withSpring(0, { stiffness: 65 })),
},
],
}),
[],
);
const animatedStylesRemove = useAnimatedStyle(
() => ({
transform: [
{
translateY: withSequence(withTiming(0), withDelay(5000, withSpring(-100))),
},
],
}),
[],
);
const getStatusLabel = () => {
switch (status) {
case false:
return 'connection failed'
case true:
return 'connected'
default:
return 'connected';
}
};
return (
<Animated.View style={[{}, !status ? animatedStyles : animatedStylesRemove]}>
<HealthStatusContainer>
<Dot status={status} />
<StatusLabel>{getStatusLabel()}</StatusLabel>
</HealthStatusContainer>
</Animated.View>
);
};
export default HealthCheckStatus;
My problem here is that I'm forced to use two different animations depending on the state. How can I stop the animation until a state change? Or if the state changes, play the animation backward. If the state is false, do that ... then if the state becomes true (after it was false) do that in reverse.
Thanks a lot for your help.
I've tried to use conditional rendering in the animated style but it's not possible.