How to use SetTimeout in React Native?

04-Apr-2023

.

Admin

How to use SetTimeout in React Native?

Hi Guys,

Hello all! In this article, we will talk about how to use setTimeout in react native. I would like to show you how to create setTimeout in react native. if you want to see example of react native setTimeout example then you are a right place. I explained simply step by step how to implement setTimeout in react native. you will do the following things for setTimeout in react native.

Sometimes, we may need to execute code after some delay. In such cases, we use JavaScript method setTimeout in React Native. SetTimeout method is used to execute a function after waiting a specific amount of time.

Let's start following example:

Step 1: Download Project


In the first step run the following command to create a project.

expo init ExampleApp

Step 2: App.js

In this step, You will open the App.js file and put the code.

import React from 'react';

import { Button, Image, StyleSheet, View} from 'react-native';

const App = () => {

const [image, setImage] = React.useState(null);

const showImage = () => {

setTimeout(() => {

setImage('https://www.nicesnippets.com/image/nice-logo.png');

}, 3000);

}

return (

<View style={styles.container}>

<View style={styles.imageContainer}>

<Image

source={{ uri: image }}

style={{ width: '100%', height: 160 }}

resizeMode='contain'

/>

</View>

<View style={styles.buttonContainer}>

<Button

title='Show Image'

onPress={() => showImage()}

/>

</View>

</View>

);

}

const styles = StyleSheet.create({

container: {

flex: 0.85,

justifyContent: 'center',

paddingHorizontal: 10,

},

buttonContainer: {

marginTop: 10,

},

imageContainer: {

justifyContent: 'center',

alignItems: 'center',

},

});

export default App;

Run Project

In the last step run your project using the below command.

expo start

You can QR code scan in Expo Go Application on mobile.

Output :

It will help you...

#React Native