DEV Community

Discussion on: πŸ”₯ React Hooks.😎

Collapse
 
leandroreschke profile image
Leandro Reschke

Awesome! You explained it so well, that only now I understand useEffect.

I have a suggestion, could you also show wrong cases? For example when you shouldn't use useState and instead use a normal variable, or when you shouldn't use useEffect and instead only a normal function, maybe it will help understand it even better.

Sometimes I struggle to decide if I need it or not, like when making a Login screen, it's going to check with an API if the user is valid before, should I use useEffect? Or a normal function tied to a form and set a loading animation.

Anyway, great article, thank you!

Collapse
 
dawoodxp97 profile image
Shaik Dawood

Thank you 😊.

Yes, nice suggestion. I'll make sure to update the Bad Practices of Hooks in a separate blog stay tuned for it.

The use of useEffect will depends on requirement, basically the useEffect is used to execute a particular code that needs to happen during lifecycle of the component instead of on specific user interactions or DOM events.

As in your case, I'd definitely suggest you to use useEffect for checking the user login with API. Make sure to use the side effect as "Empty Dependency Array" in useEffect. So it will run once the app completes initial Render.

Hope you've got an Idea about it.. Thank You.