react-lifecycle-hook
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

React Lifecycle Hook

Lifecycle hooks for functional components

Installation

Open a Terminal in the project root and run:

yarn react-lifecycle-hook

or

npm i react-lifecycle-hook

API reference

useComponentDidMount

After all the elements of the page is rendered correctly, this method is called

import { useComponentDidMount } from 'react-lifecycle-hook';

export const YourAwesomeComponent = () => {
  useComponentDidMount(() => {
    fetchData()
    document.addEventListener("click", closeMenu);
  })

  return (
    //...
  )
}

useComponentDidUpdate

Can be useful to perform some action when the state changes

import { useComponentDidUpdate } from 'react-lifecycle-hook';

export const YourAwesomeComponent = () => {
  useComponentDidUpdate(
    (prevProps) => {
      if(prevProps.isOpenModal !== isOpenModal) {
        console.log('Modal state changed')
      }
    },
    { isOpenModal },
  );

  return (
    //...
  )
}

useComponentWillUnmount

useComponentWillUnmount is invoked immediately before a component is unmounted and destroyed

import { useComponentWillUnmount } from 'react-lifecycle-hook';

export const YourAwesomeComponent = () => {
  useComponentWillUnmount(() => {
    document.removeEventListener("click", closeMenu);
  });

  return (
    //...
  )
}

Package Sidebar

Install

npm i react-lifecycle-hook

Weekly Downloads

4

Version

1.0.1

License

MIT

Unpacked Size

8.44 kB

Total Files

11

Last publish

Collaborators

  • andriibodryi