Categories
JavaScript Answers React Native Answers

How to use setTimeout in React Native?

Spread the love

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’whenloadedistrue`.

Therefore, ‘loaded’ is shown after 3 seconds.

Conclusion

To use setTimeout in React Native, we can call it directly.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *