I've created a functional component in React like this:
import React, {useEffect} from "react";
import "./styles.css";
export default function App() {
useEffect(() => logSomething(), [])
const logSomething = () => console.log('whats up');
return (<div> hello </div>)
}
This function runs without any errors, and successfully logs out "whats up" in the console.
When I try the following, however, I get a TypeError:
export default function App() {
useEffect(logSomething(), [])
const logSomething = () => () => console.log('whats up');
return (<div> hello </div>)
}
Similarly, I get a TypeError when I try this:
export default function App() {
useEffect(logSomething, [])
const logSomething = () => console.log('whats up');
return (<div> hello </div>)
}
Why does the first example succeed, but the second and third example fail? Is the first function expression getting hoisted somehow? And if so, why aren't the other 2 function expressions being hoisted? Or is this just some quirk with how function references work in JavaScript?