The effect hook lets us perform side effects in functional components. We can think of the effect hook as a replacement for the React lifecycle methods like componentDidMount, ComponentDidUpdate, and componentWillMount combined.
⠀⠀⠀⠀⠀⠀⠀⠀⠀
❌What does useEffect looks like? ⠀⠀⠀⠀
const [count, setCount] = useState(0);
⠀⠀⠀⠀⠀⠀⠀⠀⠀
useEffect(() => {
setCount(1); });
❌What does useEffect do?
⠀⠀⠀⠀⠀⠀⠀⠀⠀
When we use this hook we're telling React it needs to execute this effect after every render.
⠀⠀⠀⠀⠀⠀⠀⠀⠀
❌What if I don't want it to happen after every render? ⠀⠀⠀⠀⠀⠀⠀⠀⠀
If the desired outcome is for our effect to happen just once we can define this by passing a second argument to our effect that will be an empty array and now our effect will work as a componentDidMount. ⠀⠀⠀⠀⠀⠀⠀⠀⠀
const [count, setCount] = useState(0); ⠀⠀⠀⠀⠀⠀⠀⠀⠀
useEffect(() => {
setCount(1); }, []);
❌What if I only want it to happen if the value changes?
⠀⠀⠀⠀⠀⠀⠀⠀⠀
Easy, instead of passing an empty array as the second argument, we pass the array with the value inside which will tell React to run this effect ONLY if the value has changed.
const [count, setCount] = useState(0);
⠀⠀⠀⠀⠀⠀⠀⠀⠀
useEffect(() => {
setCount(1); }, [count]);
So this was it for today's lesson on React Hooks if you haven't go check out my post where I talk about how to useState!
⠀⠀⠀⠀⠀⠀⠀⠀⠀
💭 Have you used React Hooks? I want to hear your thoughts about them in the comment section below👇🏼👇🏼

