Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 17, 2021 08:56 pm GMT

How to useEffect()

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 state 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 we can do what we want with useEffect. You can even make an api requests with fetch.

Thanks for reading. Feel free to comment anything or correct me if I'm wrong.


Original Link: https://dev.to/palenquer/how-to-useeffect-1dnl

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To