@reactutils/use-previous
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

usePrevious

NPM

A custom hook that uses the useRef hook internally for storing the previous value.

Installation

npm install @reactutils/use-previous

# or

yarn add @reactutils/use-previous

Usage

function App() {
  // State value and setter for our example
  const [count, setCount] = useState<number>(0);
  // Get the previous value (was passed into hook on last render)
  const prevCount: number = usePrevious<number>(count);
  // Display both current and previous count value
  return (
    <div>
      <h1>
        Now: {count}, before: {prevCount}
      </h1>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

Package Sidebar

Install

npm i @reactutils/use-previous

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

2.4 kB

Total Files

6

Last publish

Collaborators

  • sepehr09