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 t…


This content originally appeared on DEV Community and was authored by Paulo Alenquer

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.


This content originally appeared on DEV Community and was authored by Paulo Alenquer


Print Share Comment Cite Upload Translate Updates
APA

Paulo Alenquer | Sciencx (2021-07-17T20:56:04+00:00) How to useEffect(). Retrieved from https://www.scien.cx/2021/07/17/how-to-useeffect/

MLA
" » How to useEffect()." Paulo Alenquer | Sciencx - Saturday July 17, 2021, https://www.scien.cx/2021/07/17/how-to-useeffect/
HARVARD
Paulo Alenquer | Sciencx Saturday July 17, 2021 » How to useEffect()., viewed ,<https://www.scien.cx/2021/07/17/how-to-useeffect/>
VANCOUVER
Paulo Alenquer | Sciencx - » How to useEffect(). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/17/how-to-useeffect/
CHICAGO
" » How to useEffect()." Paulo Alenquer | Sciencx - Accessed . https://www.scien.cx/2021/07/17/how-to-useeffect/
IEEE
" » How to useEffect()." Paulo Alenquer | Sciencx [Online]. Available: https://www.scien.cx/2021/07/17/how-to-useeffect/. [Accessed: ]
rf:citation
» How to useEffect() | Paulo Alenquer | Sciencx | https://www.scien.cx/2021/07/17/how-to-useeffect/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.