Next.js Discord

Discord Forum

Should I worry about racing function if I am calling an outside function from within useEffect

Unanswered
Virginia's Warbler posted this in #help-forum
Open in Discord
Virginia's WarblerOP
I have:
useEffect(() => {
  someFunction();
}, []);

const someFunction = async () => {}


Someone said that someFunction could be undefined. But that person isn't too reliable.
So I want to ask, is that true?
And should I really do something like
useEffect(() => {
  if (someFunction) {
    someFunction();
  }
}, []);

3 Replies

Asian black bear
It depends on the lexical scope, if the function is defined in the same scope as useEffect, but not prior to it, you risk running into issues since JavaSciprt won't necessarily hoist the function to the top.
Also, it's a huge code smell if you define a function after the effect as it's much harder to read the code that way.
Virginia's WarblerOP
Thank you