In React Native class component life cycle there are 3 inbuilt functions named as componentDidMount, componentDidUpdate, and componentWillUnmount used to perform certain task on a certain condition. But in functional these three methods are not available and to perform their task here we would use useEffect() method. The useEffect method is combined part of all three componentDidMount, componentDidUpdate, and componentWillUnmount methods. The useEffect calls each time when after rendering the screen. Even if we update State or define the method it will calls every time when render calls. To stop calling useEffect after every render we have to pass Array[] as argument in useEffect method. It will prevent the useEffect method to calling automatically. To run the useEffect once we have to pass the [] Array as Second argument so this would tell the react native that there are no state or prop needs to updated so it won’t run the useEffect again. So in this tutorial we would React Native Call useEffect Only Once in Functional Component Android iOS Example Tutorial.
Contents in this project React Native Call useEffect Only Once in Functional Component Android iOS Example Tutorial:
1. Open your project’s main App.js file and import useState, useEffect from React.
1
|
import React, { useState, useEffect } from ‘react’;
|
2. Import Alert, View, StyleSheet, Button and Text component.
1
|
import { Alert, View, StyleSheet, Button, Text } from ‘react-native’;
|
3. Creating our main export default function App(). This is our Root functional component.
1
2
3
4
|
export default function App() {
}
|
4. Creating a State named as count with state change function named as setCount. We would also set the State value as Zero. To know more about How to use State in Functional Component read my this tutorial.
1
|
const [count, setCount] = useState(0);
|
5. Creating useEffect() method and here we would set the count value as Zero again. We would also Show a alert dialog box on to see the effect on mobile screen. Now we would pass the Empty [] Array as second argument to stop useEffect to run again.
1
2
3
4
5
6
7
8
9
|
// Similar to componentDidMount and componentDidUpdate Method.
useEffect(() => {
// Set the count variable value to Zero.
setCount(0);
Alert.alert(‘useEffect Called Once Only….’);
}, [] );
|
6. Creating Return Block and here we would make our Root View component and 1 Button component. We would increase value of Count to Plus +1 on every click event.
1
2
3
4
5
6
7
8
9
10
11
|
return (
<View style={styleSheet.MainContainer}>
<Text style={styleSheet.text}> Count Value = {count} </Text>
<Button onPress={() => setCount(count + 1)} title=“Change State Value in Functional Component” />
</View>
);
|
7. Complete Source Code for App.js file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import React, { useState, useEffect } from ‘react’;
import { Alert, View, StyleSheet, Button, Text } from ‘react-native’;
export default function App() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate Method.
useEffect(() => {
// Set the count variable value to Zero.
setCount(0);
Alert.alert(‘useEffect Called Once Only….’);
}, [] );
return (
<View style={styleSheet.MainContainer}>
<Text style={styleSheet.text}> Count Value = {count} </Text>
<Button onPress={() => setCount(count + 1)} title=“Change State Value in Functional Component” />
</View>
);
}
const styleSheet = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
backgroundColor: ‘#FAFAFA’,
padding: 10
},
text:{
fontSize: 20,
paddingBottom: 20
}
});
|
Screenshots: