use-prev
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

What is this?

This custom hook allows you to get previous value from the useState

Installation vie package manager

  yarn add use-prev
  npm install use-prev

Usage

 
  import { useState } from "react";
  import usePrev from "use-prev";
 
  function MyComponent() {
 
    const [click, setClick] = useState(0);    
    const prevClick = usePrev(click);
    
    const increment = () =>
      setClick((_click) => _click + 1);
 
    return (
      <>
        <p>Current Click Value: {click}</p> // 1
        <p>Previous Click Value: {prevClick}</p> // 0
        <button onClick={increment}>
          Increment
        </button>
      </>
    )
 
  }

Package Sidebar

Install

npm i use-prev

Weekly Downloads

1

Version

0.0.2

License

MIT

Unpacked Size

4.76 kB

Total Files

9

Last publish

Collaborators

  • dkvilo