31
UseEffect: Is it really Effective?
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class.
If you have been working with class component you would have performed side effects in your component like updating Ui, fetching data from api or subscribing to any changes. Since the render method is too early to perform side effects we have lifecycle method in class component.
You must have been concerned about how useEffect would handle all of this. Let's get started without further ado.

UseEffect accept two arguments:
useEffect(() => {
//Callback
}, [dependencies]);
Let's have a look at all of the scenarios with some examples:
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>
);
If we check at the logs, we can see that the side-effect is called whenever the count changes.
LOG App.js: useEffect
LOG App.js: useEffect
LOG App.js: useEffect
LOG App.js: useEffect
useEffect(() => {
console.log('App.js: useEffect');
}, []);
If we look into logs, side-effect got called only one time
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>
);
If you closely look into console, You will find whenever the value of
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
Some side effects need a cleanup, like canceling any api call while un-mounting, closing connection or clearing timers.
We can achieve this by returning a cleanup function from
useEffect() callback
.useEffect(() => {
// This is your side-effect logic
return function cleanup() {
// Side-effect cleanup
};
},[dependencies]);
Cleanup works in following way:
useEffect()
invokes the callback having the side-effect. cleanup
function is not called.useEffect()
invokes the cleanup function from the latest side-effect.Let me show you some basic code to explain:
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>
);
If you look into the logs, cleanup function is getting called every time before invoking the next side-effect.
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.
- Callback argument is a function to put the side-effect logic.
- Dependencies is a list of dependencies of your side-effect: being props or state values.
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
- initial mounting(ComponentDidMount),
- Managing state changes (ComponentDidUpdate)
- For side-effect cleanup (ComponentWillUnmount)

I hope this post helped you understand the basic idea of useEffect(). Feel free to add your suggestions.
Follow me on Twitter.
Follow me on Twitter.
Happy coding
31