Basically my question is whether there is any runtime difference between the two ways I list below of returning a cleanup function from a useEffect call. Is there a common best practice? I didn't see anything about it in the reactjs docs so I'm curious if there is any advantage to either practice.
Specifically:
Is the call stack different aside from the extra anonymous function?
Are there circumstances where it might affect the this keyword?
Is one safer than the other?
import React, { useEffect } from 'react';
function aPlainJsFunction() {
console.log("Bar");
}
Example = () => {
useEffect(() => {
console.log("Foo");
return aPlainJsFunction; // **This Line**
}
return <Text>Example</Text>
}
-or-
import React, { useEffect } from 'react';
function aPlainJsFunction() {
console.log("Bar");
}
Example = () => {
useEffect(() => {
console.log("Foo");
return () => { // **These Lines**
aPlainJsFunction(); // **These Lines**
} // **These Lines**
}
return <Text>Example</Text>
}
Is the call stack different aside from the extra anonymous function?
No
Are there circumstances where it might affect the this keyword?
Unsure - would guess no
Is one safer than the other?
No