31
loading...
This website collects cookies to deliver better user experience
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class.
useEffect(() => {
//Callback
}, [dependencies]);
useEffect(() => {
console.log('App.js: useEffect');
});
return (
<SafeAreaView style={backgroundStyle}>
<View>
<Text style={styles.sectionTitle}>Hi There {count} times</Text>
<Button
title="Press me"
onPress={() => {
setCount(count + 1);
}}
/>
</View>
</SafeAreaView>
);
LOG App.js: useEffect
LOG App.js: useEffect
LOG App.js: useEffect
LOG App.js: useEffect
useEffect(() => {
console.log('App.js: useEffect');
}, []);
LOG Click Me
LOG Click Me
LOG Click Me
LOG Click Me
When configured in such a way, the useEffect() executes the callback just once, after initial mounting. We can say it will work like componentDidMount()
const [count, setCount] = React.useState(0);
const [countDown, setCountDown] = React.useState(100);
useEffect(() => {
console.log('App.js: useEffect');
}, [count]);
return (
<SafeAreaView style={{flex:1}}>
<View>
<Text style={styles.sectionTitle}>Hi There {count} times</Text>
<Text style={styles.sectionTitle}>Time is ticking {countDown}</Text>
<Button
title="Increment"
onPress={() => {
console.log('Increment Count');
setCount(count + 1);
}}
/>
<Button
title="Decrement"
onPress={() => {
console.log('Decrement Count');
setCountDown(countDown - 1);
}}
/>
</View>
</SafeAreaView>
);
count
changes, useEffect got called only then.LOG App.js: useEffect
LOG Decrement Count
LOG Decrement Count
LOG Decrement Count
LOG Decrement Count
LOG Increment Count
LOG App.js: useEffect
LOG Increment Count
LOG App.js: useEffect
LOG Increment Count
LOG App.js: useEffect
So you can see it will work the same way like ComponentDidUpdate work in class component
useEffect() callback
.useEffect(() => {
// This is your side-effect logic
return function cleanup() {
// Side-effect cleanup
};
},[dependencies]);
useEffect()
invokes the callback having the side-effect. cleanup
function is not called.useEffect()
invokes the cleanup function from the latest side-effect.const [count, setCount] = React.useState(0);
useEffect(() => {
console.log('App.js: useEffect');
return function cleanup() {
console.log('App.js: cleanup');
};
}, [count]);
return (
<SafeAreaView style={{flex: 1}}>
<View>
<Text style={styles.sectionTitle}>Hi There {count} times</Text>
<Button
title="Increment"
onPress={() => {
console.log('Increment Count');
setCount(count + 1);
}}
/>
</View>
</SafeAreaView>
);
LOG App.js: useEffect
LOG Increment Count
LOG App.js: cleanup
LOG App.js: useEffect
LOG Increment Count
LOG App.js: cleanup
LOG App.js: useEffect
*When we want to perform any action once, especially when the app mount first time. We can prefer useEffect. *
Let us consider an example , we want to fetch list of newsfeed while loading the newsfeed screen.
const [newsFeed, setNewsFeed] = React.useState([]);
async function fetchNewsFeed() {
const response = await fetch('/employees');
const newsFeedData = await response.json(response);
setNewsFeed(newsFeedData);
}
useEffect(() => { // can not be async
fetchNewsFeed(); // Can invoke async function
}, []);
useEffect(callback, dependencies) is the hook that manages the side-effects in functional components.
useEffect(callback, dependencies) invokes the callback after initial mounting, and on later renderings, if any value inside dependencies has changed.
useEffect(callback, dependencies) can be used in following ways