What is useEffect and what exactly this hook does?
I'm going to teach you (or at least try) these topics here in this post.
What is useEffect?
useEffect is a hook from React that receives two parameters. The first one is a function and the second one is a dependency array.
What this hook does?
There's a lot of things that you can do with this hook like trigger a function when your component is mounted or when a variable is updated. Look at this example:
useEffect(() => {
//first parameter
}, [/*second parameter*/]);
The first parameter is the function that will execute your code and the second parameter is when we want to execute this code. For example, with second parameter you can put all variables that you want and when these variables receives an update, useEffect will trigger the function.
If you want to trigger the function when your component mount, you just need to leave the array empty.
That's it! Now you can do what we want with useEffect. You can even make api requests with fetch.
Thanks for reading. Feel free to comment anything or correct me if I'm wrong.
Top comments (0)