Documentation

usePrevious

Get previous value of state for comparisons.

Installation

pnpm add @rouf-dev/pantheon-ui

Usage

"use client";

import { usePrevious } from "@rouf-dev/pantheon-ui";
import { useState } from "react";

export function CounterDemo() {
  const [count, setCount] = useState(0);
  const previousCount = usePrevious(count);

  return (
    <div>
      <p>Current: {count}</p>
      <p>Previous: {previousCount}</p>
      <button onClick={() => setCount(c => c + 1)}>
        Increment
      </button>
    </div>
  );
}

Parameters

value: Value to track