Code Monkey home page Code Monkey logo

react-lifecycle-hooks's Introduction

React Lifecycle Hooks

A Simple abstraction to useEffect and make the lifecycle using hooks more declarative

Motivation

I was coding with some newbie developers, and when i started with the new team i realized that people are getting stuck using the react lifecycle with the useEffect hook, so i replicated the methods using the useEffect hook.

Documentation

Use Update

The useUnmount hook replicates the same functionality as the componentDidUpdate heres an example bellow

import React from 'react';
import { useUpdate } from 'react-lifecycle-hooks';

interface Props {
  // your prop interface fields goes here...
}

const App = (props: Props) => {
  useUpdate((prevProps, nextProps) => {
    // place here the code for the update on the component
  }, props);
};

Params:

 - `fn`: () => Promise<void> | void

 - `props`: Receives a generic called props `<T extends Record<string, any>>`

Use Unmount

The useUnmount hook replicates the same functionality as the componentWillUnmount heres an example bellow

import React from 'react';
import { useUnmount } from 'react-lifecycle-hooks';

const App = () => {
  useUnmount(async () => {
    // place your code bellow
  });
};

Params:

 - `fn`: () => void

Use Mount

The useMount hook replicates the same functionality as the componentDidMount heres an example bellow

import React from 'react';
import { useMount } from 'react-lifecycle-hooks';

const App = () => {
  useMount(async () => {
    // place your code bellow
  });
};

Params:

 - `fn`: () => Promise<void> | void

Use Previous

This is a helper that i've created to replicate the componentDidUpdate that gets the last value for that property. i think that will be very useful for you also

import React, { useState } from 'react';
import { usePrevious } from 'react-lifecycle-hooks';

const App = () => {
  const [value] = useState('');

  const previousValue = usePrevious(value) || '';
};

Params:

- `value`: Its a Generic Type for the `value` it already infers the type

Returns:

The return will be undefined or the infered value (the type of the variable that you are passing as a parameter).

react-lifecycle-hooks's People

Contributors

lfscamargo avatar

Stargazers

Paulo Eduardo Rezende avatar Matheus Tadeu avatar

Watchers

James Cloos avatar  avatar

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.