usePrevious

usePrevious gives you the value of your last state.

Current Value: 1

Previous Value: undefined

Code for example above

import { useState } from "react";
import { usePrevious } from "@talves/use-previous";
export const Example = (props) => {
const [value, setValue] = useState(1);
const lastValue = usePrevious(value);
return (
<div>
<button onClick={() => setValue(value + 1)}>
Click here to increment
</button>
<h2>{`Current Value: ${value}`}</h2>
<h2>{`Previous Value: ${lastValue}`}</h2>
</div>
);
};