Git Product home page Git Product logo

mohcinenazrhan / react-use-lifecycle-helpers Goto Github PK

View Code? Open in Web Editor NEW
3.0 4.0 2.0 516 KB

Helpers functions arround useEffect hook to make your life easier.

Home Page: https://www.npmjs.com/package/react-use-lifecycle-helpers

License: MIT License

HTML 5.15% JavaScript 92.64% CSS 2.21%
useeffect-hook hook lifecycle react componentdidupdate componentdidmount componentwillunmount usedidupdate usedidmount usewillunmount

react-use-lifecycle-helpers's Introduction

react-use-lifecycle-helpers

Helpers functions arround useEffect hook to make your life easier, providing the most use cases of useEffect hook, among them the lifecycle of class component.

NPM JavaScript Style Guide

Install

npm install --save react-use-lifecycle-helpers

See Example

Usage

useDidMount

This hook is a replacement for the componentDidMount method.

import useLifecycleMethods from "react-use-lifecycle-helpers";

export default function MyComponent() {
  const { useDidMount } = useLifecycleMethods();

  useDidMount(() => {
    console.log("MyComponent is mounted");
  });
}

useDidUpdate

This hook is similar to the componentDidUpdate method.

import useLifecycleMethods from "react-use-lifecycle-helpers";

export default function MyComponent(props) {
  const [state, setState] = useState({ count: 0, bool: false });

  const { useDidUpdate } = useLifecycleMethods(state, props);

  useDidUpdate(() => {
    console.log("MyComponent is updated");
  });
}

useWillUnmount

A hook that's a replacement for the componentWillUnmount method.

import useLifecycleMethods from "react-use-lifecycle-helpers";

export default function MyComponent() {
  const { useWillUnmount } = useLifecycleMethods();

  useWillUnmount(() => {
    console.log("MyComponent will unmount");
  });
}

useDepsDidChange

Track multiple or one of the state properties.

import useLifecycleMethods from "react-use-lifecycle-helpers";

export default function MyComponent(props) {
  const [state, setState] = useState({ count: 0, bool: false });

  const { useDepsDidChange } = useLifecycleMethods(state, props);

  useDepsDidChange(
    prevState => {
      console.log("useDepsDidChange Count", state.count, prevState.count);
    },
    ["count"]
  );

  useDepsDidChange(
    prevState => {
      console.log("useDepsDidChange Bool", state.bool, prevState.bool);
    },
    ["bool"]
  );

  useDepsDidChange(
    prevState => {
      console.log("useDepsDidChange Count", state.count, prevState.count);
      console.log("useDepsDidChange Bool", state.bool, prevState.bool);
    },
    ["bool", "count"]
  );
}

License

MIT © [Mohcine NAZRHAN](https://github.com/Mohcine NAZRHAN)

Contributors

Todo

  • Migrate to TypeScript

react-use-lifecycle-helpers's People

Contributors

mohcinenazrhan avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-use-lifecycle-helpers's Issues

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.