@martinstark/use-once

Roughly a react hook equivalent of componentWillMount

Usage no npm install needed!

<script type="module">
  import martinstarkUseOnce from 'https://cdn.skypack.dev/@martinstark/use-once';
</script>

README

useOnce

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.

Usage

// Without return value

const runBeforeFirstRender = () => {};

const Component = () => {
  useOnce(runBeforeFirstRender);

  return <></>;
};

// With return value

const runBeforeFirstRender = () => "result";

const Component = () => {
  const r = useOnce(runBeforeFirstRender);

  return <>{r}</>;
};