HooksuseMount

useMount()

The useMount() hook is a semantic alias for useEffect(fn, []). It ensures that the provided callback runs strictly once when the component is first mounted to the DOM.

Import

typescript
import { useMount } from 'the-react-hooks';

Usage

Toggle the component to trigger the useMount hook.

Component is not in DOM
Component.tsx
import { useMount } from 'the-react-hooks';

export const Component = () => {
  useMount(() => {
    console.log('Component Mounted!');
  });

  return (
    <div>
      Check the console logs.
    </div>
  );
}

API

Arguments

  • effect EffectCallback The function to execute on mount. It can optionally return a cleanup function.

Returns

This hook returns void.