componentdidmount() method used in react native class component coding structure. In react native class component the componentdidmount() called after render() method called. The main purpose of componentdidmount() is to parse JSON or do some changes on screen after application start. After coming of react native functional component all the mount methods is replaced with single useEffect() method. The single useEffect() method can works as all the mounting methods. So in this tutorial we would learn about How to Use useEffect as componentdidmount behavior in React Native Hooks Android iOS Example Tutorial.
Contents in this project How to Use useEffect as componentdidmount behavior in React Native Hooks Android iOS Example Tutorial:
1. Open your project’s main App.js File and import useState, useEffect from react in your project .
1
|
import React, { useState, useEffect } from ‘react’;
|
2. Import Alert, View, StyleSheet, Button and Text component in your project.
1
|
import { Alert, View, StyleSheet, Button, Text } from ‘react-native’;
|
3. Create our main export default function App(). This is our main Root functional component.
1
2
3
4
5
|
export default function App() {
|
|
4. Creating a State named as count with State changing function named setCount. We would also set the count State default value as Zero.
1
|
const [count, setCount] = useState(0);
|
5. Creating useEffect() method. We would pass empty array as argument in useEffect() function to execute this function once only. Using this it works same as componentdidmount.
1
2
3
4
5
6
7
8
9
|
// Similar to componentDidMount.
useEffect(() => {
// Set the count State Value to Seven.
setCount(7);
Alert.alert(‘useEffect Called As componentDidMount’);
}, [] );
|
Screenshot:
6. Creating return block. Here we would make a Text component and Button component and change the State value on button click.
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” />
</View>
);
|
7. Creating Style.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
const styleSheet = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
backgroundColor: ‘#FAFAFA’,
padding: 10
},
text:{
fontSize: 20,
paddingBottom: 20
}
});
|
8. 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.
useEffect(() => {
// Set the count State Value to Seven.
setCount(7);
Alert.alert(‘useEffect Called As componentDidMount’);
}, [] );
return (
<View style={styleSheet.MainContainer}>
<Text style={styleSheet.text}> Count Value = {count} </Text>
<Button onPress={() => setCount(count + 1)} title=“Change State Value” />
</View>
);
}
const styleSheet = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
backgroundColor: ‘#FAFAFA’,
padding: 10
},
text:{
fontSize: 20,
paddingBottom: 20
}
});
|
Screenshots: