I am looking to have something run once when the react app first loads and only the one time. At first, one is inclined to make use of the useEffect hook:
useEffect(() => {
console.log("Effect hook.");
}, []);
The problem with this approach is that when I put it in my App component, the useEffect hook runs AFTER mounting. Subsequently, the following will fail because stuff won't be defined when the child component renders:
function App() {
let stuff;
// Define the supported configs on mount. This will only run at mount time.
useEffect(() => {
console.log("Effect hook.");
stuff = getSomeStuff();
}, []);
//
console.log("About to return.");
return (
<div>
<ComponentThatNeedsStuff stuff={stuff} />
</div>
);
}
and you will see the following printed:
About to return.
Effect hook.
If you only want the function getSomeStuff to run once but you need the stuff for child components, what is the best way to handle that?