Accepts a variable (possibly a prop or a state) and returns its previous render's value.
- You want to keep track of the changes of your component state/props to decide whether it should update or not
import { useState } from 'react';
import { useInterval, usePreviousValue } from 'beautiful-react-hooks';
const TestComponent = () => {
const [seconds, setSeconds] = useState(0);
const prevSeconds = usePreviousValue(seconds);
useInterval(() => setSeconds(1 + seconds), 1000);
return (
<DisplayDemo>
<p>{seconds}s</p>
<p>The previous value of the state 'seconds' was: {prevSeconds}</p>
</DisplayDemo>
);
};
<TestComponent />