@martinstark/use-once
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

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}</>;
};

Package Sidebar

Install

npm i @martinstark/use-once

Weekly Downloads

0

Version

1.0.4

License

MIT

Unpacked Size

6.52 kB

Total Files

6

Last publish

Collaborators

  • martinstark