In this post let's talk about the paradigm shift from lifecycle methods to state synchronization and hooks in ReactJs.
When class components were a thing in ReactJs (Well it still is, but no one likes them anymore), we use to think a lot in terms of mouting, unmounting, and lifecycle methods.
Whenever a class component mounts, the lifecycle methods are called in following sequence: constructor β render β DOM and refs update β componentDidMount
But then came React Hooks, and we started to think in terms of dependency array.
And, often times we ask:
What is the hooksΒ equivalentΒ toΒ [some lifecycle method]?
The quick answer is that hooks are a paradigm shift from thinking in terms of "lifecycles" to thinking in terms of "state and synchronization with DOM".
Trying to take the old paradigm and apply it to hooks just doesn't work out very well and can hold you back.
useEffect(fn) // fn invoked on all updates
useEffect(fn, []) // invoked on mount
useEffect(fn, [a, b, c]) // invoked if any of the members of the array are updated
The above snippet is not the right way to think about React hook.
Lifecycles are gone, but we still think of useEffect with empty dep array as componentDidMount, and thatβs not the right way to think about React Hooks.
Now I want to present to you the right way to think about react hooks.
State Synchronization
See, the question is not "when does this effect run" the question is "which state does this effect synchronizes with"
useEffect(fn) // useEffect with no dep array, sync with all state
useEffect(fn, []) // useEffect with empty dep array, sync with no state
useEffect(fn, [stateA, stateB]) // useEffect with stateA and stateB in dep array sync with stateA and stateB.
And thatβs how you should think about React Hooks.
Hope, you found this video useful, and if so, make sure show your support by subscribing.
I also run a weekly newsletter, so you can join me there also: https://swastikyadav.com
Thank You!
Top comments (13)
Ah! Now I got your point. Now I see where you coming from.
I missUnderstood your point of miss-using the useEffect hook.
Well, the whole point to ReactJs is to keep the UI and State in sync. And useEffect is the most commonly used hook to handle side-effects in ReactJs community.
useEffect can create performance issue without dependency array, but with dep array, I think it works just fine.
Would love to know your view on this, if you are ok to elaborate your point a little bit more.
Thank You π
Wow, this is really very insightful. I will give this a thorough study.
Thanks for sharing this.
And thanks for starting this positive and constructive conversation.
π
This short and succinct article was one of the most enlightening ones I've read about the useEffect hook. I've subscribed to your Youtube channel and newsletter, can't wait to see more from you!
It can't be a coincidence that you wrote this just a few days after David Khourshid's (creator of XState library) talk about this exact topic, which include the same insights in this comment thread. If you haven't checked it out, you really should as I believe you'll benefit a lot from it, and many of David's talks: youtube.com/watch?v=HPoC-k7Rxwo
His conclusion is the same as Luke's - avoid useEffect hook as side-effects should be considered as part of state management ([state, event]=>[nextState, effects]) and not separately, which is what external libraries help you do. This is one of the things that David's own XState library does on top of implementing a declarative and explicit way of managing state with State Machines. You should also check out his talks on state machines and his XState library.
Thanks for all your support.
I will surely check David Khourshid's talk on this topic.
Agreed
So, how would you go about making an API call, based on the response of which your view will be rendered.
As per my React knowledge useEffect is the only way here.
Please share if it can be done without useEffect.
Nicely explained!
Thanks for the kind words K-Sato π
Thank you for clearing it up!
My pleasure... π
Nice article.
Thank you, Andrew π
ReactJs is a popular front-end development framework that has seen a lot of growth in the past few retro bowl years. It's easy to use and has a lot of features, making it a great choice for web applications and apps.