Sometimes, we want to use setTimeout
in React Native.
In this article, we’ll look at how to use setTimeout
in React Native.
How to use setTimeout in React Native?
To use setTimeout
in React Native, we can call it directly.
For instance, we write:
import * as React from 'react';
import { ScrollView, View, Text } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';
import { Dimensions } from 'react-native';
export default function App() {
const [loaded, setLoaded] = React.useState(false);
React.useEffect(() => {
setTimeout(() => {
setLoaded(true);
}, 3000);
}, []);
return <View>{loaded && <Text>loaded</Text>}</View>;
}
We call setTimeout
with a callback that calls setLoaded
to set loaded
to true
in 3000 milliseconds.
Then we show ‘loaded’when
loadedis
true`.
Therefore, ‘loaded’ is shown after 3 seconds.
Conclusion
To use setTimeout
in React Native, we can call it directly.