1

I've searched for an answer to this question but I can see to find what I need. In a single component, can useEffect() be used more than once?

For Example:

export const MyDataForm = (props) => { ... useEffect(()=>console.log(var1),[var1]); useEffect(()=>console.log(var2),[var2]); return (<></>); } 

Is this legal, recommended, not recommended? Thanks!

2
  • Yes. Legal, and often useful. Commented Dec 22, 2021 at 21:22
  • So I have come to find out. Thanks for the response. Commented Dec 22, 2021 at 21:23

3 Answers 3

1

Yes, it is permitted, and recommended depending upon your use case.

useEffect() allows your component to subscribe to changes to a specific state. The dependencies you pass in the array of the second argument are those you wish to subscribe to within the callback. It doesn't make sense to subscribe to all of them in a single useEffect().

For instance, let's say you have a user and invoices state variable coming from your Redux store. You may wish to perform an action when user changes, but not when invoices does, and conversly when invoices changes but not user. If you add both of these entities to the dependency array of a single useEffect() hook, the callback when will be executed when either of the entities changes, not both of them. This can lead to unnecessary re-renders.

You should add two separate useEffect() hooks to achieve this, for example:

useEffect(() => { // Executed when user changes }, [ user ]); useEffect(() => { // Executed when invoices changes }, [ invoices ]); useEffect(() => { // Executed when either user OR invoices changes }, [ user, invoices ]); 
Sign up to request clarification or add additional context in comments.

Comments

1

If it makes sense, there's no problem in using useEffect more than once. Especially in your case where you have different dependencies for each hook.

4 Comments

This does not explain why you need to use multiple useEffect() hooks.
BemM - I don't ask why I NEEDED to use multiple hooks. I wanted to know if it could be done.
In which case, running your code would have answered your question.
Code running successfully means nothing about good practices
0

Simply - yes, you can use as many useEffects as you want.

Given you have individual deps for each hook you likely should be seperating the logic.

I didn't see anything in the React docs but they do speak about using multiple useEffect and useState hooks [here][1] if that gives you a little more confidence you're doing the right thing.

Personally I've use multiple useEffect hooks both in personal projects and in a professional environment.

[1]: https://reactjs.org/docs/hooks-rules.html#:~:text=Explanation,Form()%20%7B%20%2F%2F%201.&text=Use%20an%20effect%20for%20persisting,%3B%20%7D)%3B%20%2F%2F%203.

3 Comments

This does not explain why you need to use multiple useEffect() hooks.
That isn't what he asked.
I didn't need to know why I need them. I just wanted to know if it could be done.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.