My experience with React hooks have been amazing. In this article, I will precisely talk on one of the most prominent hooks that we use on a daily basis which is useEffect.
My experience with React hooks have been amazing. In this article, I will precisely talk on one of the most prominent hooks that we use on a daily basis which is useEffect.
I am assuming the readers coming to this article has good understanding on useEffect. If not, please first go through the concept at reactjs.org.
So essentially, useEffect react to changes in dependency list. They have replaced _componentDidMount, componentDidUpdate, componentWillUnmount and componentWillReceiveProps in _class based react components.
It’s very common to react to changes in props values or state values over the lifetime of a component. we also need to compare previous values and current values when reacting to changes often. In the class based component, we had componentDidUpdate for the similar use-cases.
It has following interface:
componentDidUpdate(prevProps, prevState, snapshot)
You have access to prevProps(previous props) and_ prevState(previous state)_, which can be used to make comparisons and react appropriately.
What about useEffect, how would you do those kind of comparison with them?
You can have a usePrevious custom hook which will always give you the previous value and that you make use in your useEffect to complete the comparison. Let’s see some code.
Notice, how usePrevious hook helps us by keeping a track of previous value. Now think about scenario, when there are more than one dependency in our useEffect. We need to make use of _usePrevious _hook that many times or we have to re-define our usePrevious hook to track an array of dependency.
Isn’t it too much of work every time ?
Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.
Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...
In this post, I will share my own point of view about React Hooks, and as the title of this post implies, I am not a big fan.
In this article, you will learn what are hooks in React JS? and when to use react hooks? Also, we will see the react hooks example.
React hooks tutorial for beginners, learn React hooks step by step: Introduction, useState Hook, useState with previous state, useState with object, useState with array, useEffect Hook, useEffect after render, Conditionally run effects, Run effects only once, useEffect with cleanup, useEffect with incorrect dependency, Fetching data with useEffect, useContext Hook, useReducer Hook, useReducer, Multiple useReducers, useReducer with useContext, Fetching data with useReducer, useState vs useReducer, useCallback Hook, useMemo Hook, useRef Hook