https://github.com/lfscamargo/react-lifecycle-hooks
A Simple lifecycle abstraction to useEffect to reply lifecycle
https://github.com/lfscamargo/react-lifecycle-hooks
Last synced: 2 days ago
JSON representation
A Simple lifecycle abstraction to useEffect to reply lifecycle
- Host: GitHub
- URL: https://github.com/lfscamargo/react-lifecycle-hooks
- Owner: LFSCamargo
- Created: 2020-09-25T08:04:09.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-04T01:55:05.000Z (almost 6 years ago)
- Last Synced: 2025-10-20T09:47:07.404Z (9 months ago)
- Language: JavaScript
- Size: 110 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README

# 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
```tsx
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:
```tsx
- `fn`: () => Promise | void
- `props`: Receives a generic called props `>`
```
## Use Unmount
The `useUnmount` hook replicates the same functionality as the `componentWillUnmount` heres an example bellow
```tsx
import React from 'react';
import { useUnmount } from 'react-lifecycle-hooks';
const App = () => {
useUnmount(async () => {
// place your code bellow
});
};
```
### Params:
```tsx
- `fn`: () => void
```
## Use Mount
The `useMount` hook replicates the same functionality as the `componentDidMount` heres an example bellow
```tsx
import React from 'react';
import { useMount } from 'react-lifecycle-hooks';
const App = () => {
useMount(async () => {
// place your code bellow
});
};
```
### Params:
```tsx
- `fn`: () => Promise | 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
```tsx
import React, { useState } from 'react';
import { usePrevious } from 'react-lifecycle-hooks';
const App = () => {
const [value] = useState('');
const previousValue = usePrevious(value) || '';
};
```
### Params:
```md
- `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).