useLifecycle
useLifecycle
is not one hook but three – useMount
, useUpdate
and
useUnmount
. They are hooks that can be used as replacements for lifecycle
hooks found on class components.
Usage
import React from 'react';import { useMount, useUpdate, useUnmount } from '@fransvilhelm/hooks';const Lifecycles = () => {useMount(() => console.log('Mounted'));useUpdate(() => console.log('Updated'));useUnmount(() => console.log('Unmounted'));return <p />;};
Example
Check console for logs of the lifecycle events.
Count: 0
Parameters
useMount
useMount
accepts a callback function that will be called once directly after
initial render of the component.
useUpdate
useUpdate
accepts a callback function that will be called on every update
happening on the component (state or props updated). It's practically
useEffect
but without the initial call on first render.
useUnmount
useUnmount
accepts a callback function that will be called once in connection
to the component being unmounted.