React hook for executing code once, before initial render, similar to componentWillMount.
MIT License
Tiny typescript hook for running a segment of code once, immediately before the render is committed to the screen.
It is the rough equivalent of componentWillMount
using React hooks.
See explanation.
Note: this is not necessarily a recommended pattern, but it is a question that comes up every so often.
// Without return value
const runBeforeFirstRender = () => {};
const Component = () => {
useOnce(runBeforeFirstRender);
return <></>;
};
// With return value
const runBeforeFirstRender = () => "result";
const Component = () => {
const r = useOnce(runBeforeFirstRender);
return <>{r}</>;
};