Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason to use useEffect without dependency array?

So, when we use useEffect without a dependancy array, it happens on every render.

But that's what would happen if I just wrote the code directly into the component. So is there a reason to use it?

One thing I can think of is doing something with the cleanup function, but I can't think up a valid usecase.

like image 729
Misamoto Avatar asked Nov 15 '20 00:11

Misamoto


People also ask

What happens if you don't use dependency array in useEffect?

So what happens when the dependency array is empty? It simply means that the hook will only trigger once when the component is first rendered. So for example, for useEffect it means the callback will run once at the beginning of the lifecycle of the component and never again.

Can we use useEffect without dependency?

When I was just starting with react, the issue I faced was, useEffect hook of react got called every time state or props got changed. In the above snippet, there is no dependency array so this will be called every time if state or props changes.

Why is useEffect necessary?

The motivation behind the introduction of useEffect Hook is to eliminate the side-effects of using class-based components. For example, tasks like updating the DOM, fetching data from API end-points, setting up subscriptions or timers, etc can be lead to unwarranted side-effects.

Should I always use useEffect?

Always use useEffect for asynchronous tasks Instead of writing asynchronous code without useEffect that might block the UI, utilizing useEffect is a known pattern in the React community — especially the way the React team has designed it to execute side effects.


2 Answers

But that's what would happen if I just wrote the code directly into the component.

Actually, that is not entirely true.

For example if you update a useState to the same value, React will reevaluate the function component but will not trigger effects, it will cause the code outside useEffect to execute, but not the one inside useEffect.

This is said in the official docs, Bailing out of a state update

And this is an example of the matter.

like image 129
BraisC Avatar answered Oct 22 '22 01:10

BraisC


The thing is both a i.e, a normal JS-function and a useEffect without any dependency does the same work but the difference is that :

  1. useEffect is only accessible in a React code not normal JS. It has more power in terms of react. Hence, Hooks scope is limited to the React code world.
  2. In the class-based components, the Hooks won't work but regular functions will.
  3. React will remember the function you passed (we'll refer to it as our “effect”), and call it later after performing the DOM updates but the same might not be true in other cases.(Important)
  4. By default, effects run after every completed render, but you can choose to fire them only when certain values have changed but this is not something that's easy with normal JS-functions (Just an side advantage apart from the question)
like image 27
Gayatri Dipali Avatar answered Oct 21 '22 23:10

Gayatri Dipali