Skip to main content

usePrevious

Description#

Get the previous value of a state value.

Usage#

import { useState } from 'react';import { usePrevious } from '@springtree/eva-suite-react-hooks';
const Example = () => {  const [count, setCount] = useState<number>(0);  const previousCount = usePrevious(count);
  return (    <>      <div>        {`Current count: ${count}`}      </div>      <div>        {`Previous count: ${previousCount}`}      </div>      <button onClick={() => setCount((state) => state - 1)}>Decrease</button>      <button onClick={() => setCount((state) => state + 1)}>Increase</button>    </>  )}
export default Example;

References#