Giter Site home page Giter Site logo

bettertyped / react-lifecycle-hooks Goto Github PK

View Code? Open in Web Editor NEW
22.0 1.0 2.0 1.08 MB

๐Ÿงฉ React Lifecycle Hooks - allows to useDidMount, useDidUpdate, useWillUnmount, useDidRendered lifecycle hooks with no external dependencies

License: MIT License

TypeScript 97.88% Shell 2.12%
react hooks lifecycle use did mount update unmount render hook

react-lifecycle-hooks's Introduction

๐Ÿงฉ React Lifecycle Hooks

About

React lifecycle turned into dev friendly and readable hooks

Key Features

๐Ÿ”ฎ Simple usage

๐Ÿš€ Fast and light

๐Ÿ’Ž No external dependencies

๐Ÿช„ Increases code readability

๐ŸŽŠ SSR Support

Installation

npm install --save @better-hooks/lifecycle

or

yarn add @better-hooks/lifecycle

Examples

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

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

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

  // Method for the component rerendering
  const forceUpdate = useForceUpdate()

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

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

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

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

  // Called when dependencies change, we can inspect previous dependencies
  useDidChange((prevProps) => {
    if(prevProps[0].value !== props.value) {
      // ...
    }
  }, [props], true)

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


  return (
    // ...
  )
}

react-lifecycle-hooks's People

Contributors

imlunahey avatar prc5 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-lifecycle-hooks's Issues

Example shows invalid types

Following the example for useDidChange from the README doesn't seem to work. It looks like there might be a type mismatch. ๐Ÿค”

The null we can easily fix with prevProps?.[0].value !== props.value, it's the callback not matching that I'm stuck on.

export const Example: React.FC<{ value: number }> = (props) => {
  // Called when dependencies change, we can inspect previous dependencies
  useDidChange(
    // Argument of type '(prevProps: PropsWithChildren<{ value: number; }>[] | null) => void' is not assignable to parameter of type 'VoidFunction | ((previousDependencies: PropsWithChildren<{ value: number; }>[] | null) => VoidFunction)'.
    // Type '(prevProps: PropsWithChildren<{ value: number; }>[] | null) => void' is not assignable to type 'VoidFunction'.
    // Target signature provides too few arguments. Expected 1 or more, but got 0.
    // @ts-expect-error
    (prevProps) => {
      // 'prevProps' is possibly 'null'
      // @ts-expect-error
      if (prevProps[0].value !== props.value) {
        // ...
      }
    },
    [props],
    true
  );

  return <div>Test..</div>;
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.