Documentation
usePrevious

usePrevious

usePrevious is a hook track the previous value of a variable.

Install

To start using usePrevious, you can install the @react-awesome/use-previous library or you can import it directly from @react-awesome/components if you have installed it before. In your project directory, run the following command to install the dependencies:

npm i @react-awesome/use-previous

Usage

  • 👉 Current Value0
  • 👉 Previous value0
import { usePrevious } from '@react-awesome/use-previous'
 
const Example = () => {
  const [state, setState] = useState(0)
  const previousValue = usePrevious(state)
 
  return (
    <button onClick={() => setState((prev) => ++prev)}>Click to add</button>
  )
}

Parameters

The usePrevious takes the following parameters:

value

  • Type: any