@better-typed/react-lifecycle-hooks
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

@better-typed/react-lifecycle-hooks

NPM npm bundle size npm type definitions NPM npm GitHub stars

React lifecycle turned into dev friendly hooks

Features

  • 🚀 Simple, fast and light
  • 🏭 No external dependencies

Install

npm install --save @better-typed/react-lifecycle-hooks

or

yarn add @better-typed/react-lifecycle-hooks

Usage

import React from "react";
import { useDidMount, useDidRender, useDidUpdate, useWillUnmount,useIsMounted, useWillMount } from "@better-typed/react-lifecycle-hooks";

const MyComponent: React.FC = () => {
  const [isOpen, setIsOpen] = React.useState(false)

  // returns ref with the mounted boolean state
  const mounted = useIsMounted()

  // Called before mount
  useWillMount(() => {
    // ...
  })

  // Called on component mount
  useDidMount(() => {
    // ...
  })

  // Called second, when initial DOM changes are triggered
  useDidRender(() => {
    // ...
  })

  // Called when isOpen change
  useDidUpdate(() => {
    // ...
  }, [isOpen])

  // Called when isOpen change but also on mount
  useDidUpdate(() => {
    // ...
  }, [isOpen], true)

  // Called last
  useWillUnmount(() => {
    // ...
  })


  return (
    // ...
  )
}

/@better-typed/react-lifecycle-hooks/

    Package Sidebar

    Install

    npm i @better-typed/react-lifecycle-hooks

    Weekly Downloads

    619

    Version

    1.0.5

    License

    MIT

    Unpacked Size

    34.2 kB

    Total Files

    11

    Last publish

    Collaborators

    • geras_nyx
    • albelew
    • prc5