usePrevious

React state hook that returns the previous state as described in the React hooks FAQ.

Usage

import { usePrevious } from "@reactuses/core";

const Demo = () => {
  const [count, setCount] = useState(0);
  const prevCount = usePrevious(count);

  return (
    <div>
      <button onClick={() => setCount(count + 1)}>+</button>
      <button onClick={() => setCount(count - 1)}>-</button>
      <p>
        Now: {count}, before: {prevCount}
      </p>
    </div>
  );
};

Example

Now: 0, before:

Type Declarations

export default function usePrevious<T>(state: T): T | undefined