0.0.2 • Published 5 years ago

use-prev v0.0.2

Weekly downloads
82
License
MIT
Repository
github
Last release
5 years ago

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>
      </>
    )

  }