Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two useEffect vs two API calls inside one useEffect

When component mounts I need to get data from two API endponints. For now I have:

  useEffect(() => {
    dispatch(loadSomeDataOne());
  }, [dispatch]);

  useEffect(() => {
    dispatch(loadSomeDataTwo());
  }, [dispatch]);

I was thinking to do it within one useEffect:

  useEffect(() => {
    dispatch(loadSomeDataOne());
    dispatch(loadSomeDataTwo());
  }, [dispatch]);

Is there some difference between these two solutions? Which one's better?

like image 990
heisenberg7584 Avatar asked Sep 25 '26 14:09

heisenberg7584


1 Answers

Nope. using multiple useEffect is mostly for subscribing the side effects to different variable updates. For example you might have:

  useEffect(() => {
    dispatch(loadSomeDataOne(varA));
  }, [varA]);

  useEffect(() => {
    dispatch(loadSomeDataTwo(varB));
  }, [varB]);

This way, only the desired useEffect executes when either varA or varB is updated. If you want everything to run at the same time, then you just need 1 useEffect.

like image 54
Bill Avatar answered Sep 28 '26 03:09

Bill