A Simple abstraction to useEffect and make the lifecycle using hooks more declarative
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.
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);
};
- `fn`: () => Promise<void> | void
- `props`: Receives a generic called props `<T extends Record<string, any>>`
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
});
};
- `fn`: () => void
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
});
};
- `fn`: () => Promise<void> | void
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) || '';
};
- `value`: Its a Generic Type for the `value` it already infers the type
The return will be undefined or the infered value (the type of the variable that you are passing as a parameter).