HooksusePrevious

usePrevious

A React hook that returns the previous value of a state or prop.

Installation

npm install @madeinhaus/hooks

Import

import { usePrevious } from '@madeinhaus/hooks';

Parameters

  • value (T): The current value of the state or prop.

Returns

  • (T | undefined): The previous value of the state or prop, or undefined if there is no previous value.

Usage

import { usePrevious } from '@madeinhaus/hooks';
 
function MyComponent({ count }) {
  const previousCount = usePrevious(count);
 
  return (
    <div>
      <p>Current count: {count}</p>
      <p>Previous count: {previousCount}</p>
    </div>
  );
}

In this example, we import the usePrevious hook from the @madeinhaus/hooks package and use it to get the previous value of a count prop.