1

Let's say I have two React components, A and B. Both components have a useEffect that 'listens' to the same condition (condition1, which could be a global variable, as in some React Context). When the context variable changes, which component's useEffect() is executed first?

Component A { React.useEffect(() => { do something }, [condition1]); }

Component B { React.useEffect(() => { do something else }, [condition1]); }

2
  • 4
    It would depend on the hierarchy of said components. Are they siblings or parent/child? Commented Jan 5, 2020 at 0:58
  • Hi, that makes sense. The components are siblings, but Component A is rendered before Component B, and I notice that Component A's useEffect is triggered first. Pseudo code: return ( <div> <ComponentA> <ComponentB> </div> ) Commented Jan 5, 2020 at 18:23

1 Answer 1

1

Thank you, Chris. You've answered this question. When multiple components reference the same condition in a conditional useEffect(), whichever component is higher-up in the hierarchy will have its useEffect executed first.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.