0

I know that in React you can pass an empty array to make useEffect() only run on initial load:

useEffect(() => {
  // something done once
}, []);

Now my question is, should I do everything I need under one initial useEffect:

useEffect(() => {
  // something done once
  // something else done once
}, []);

or can I separate them out:

useEffect(() => {
  // something done once
}, []);

useEffect(() => {
  // something else done once
}, []);

And if I can use either, does it matter which I choose (performance, readability, best practices).

Trees4theForest
  • 1,267
  • 2
  • 18
  • 48
  • 1
    Depends on the scenario but you can definitely separate into two different `useEffect` if you want. – norbitrial May 31 '20 at 13:07
  • Whoever voted to close, this is NOT the same question. That discusses separating concerns, with different triggers. It does NOT address whether it's allowable, or recommendable to have to effects dealing with the same concern (initial load). – Trees4theForest May 31 '20 at 15:01

0 Answers0