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]); }

Jorge
  • 21
  • 2
  • 4
    It would depend on the hierarchy of said components. Are they siblings or parent/child? – Chris Jan 05 '20 at 00: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 (
    )
    – Jorge Jan 05 '20 at 18:23

1 Answers1

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.

Jorge
  • 21
  • 2