React Hooks - 0 vs. empty array as second argument in useEffect
This is the signature for the useEffect function
export function useEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
): void {
const dispatcher = resolveDispatcher();
return dispatcher.useEffect(create, deps);
}
There you can see it only accepts an Array, void or null, so the correct answer to your question is to always send an array if you want to run it just once
Nevermind the console log error says it all:
react-dom.development.js:88 Warning: useEffect received a final argument that is not an array (instead, received
number
). When specified, the final argument must be an array.
I think using []
would be the better way because of the purpose of the second argument of useEffect
. Even if all you want to do is make sure the effect is only ran once, you should still treat the argument as an array of dependencies.