1.0.1 • Published 3 years ago

@utilityjs/use-previous-value v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

A React hook that returns a value from the previous render.

license npm latest package npm downloads types

npm i @utilityjs/use-previous-value | yarn add @utilityjs/use-previous-value

Usage

import usePreviousValue from "@utilityjs/use-previous-value";
import * as React from "react";

const useHook = () => {
  const [open, setOpen] = React.useState(false);

  const prevOpen = usePreviousValue(open);

  React.useEffect(() => {
    if (open !== prevOpen) {
      console.log("The state has been changed!");
    }
  }, [open])
};

API

usePreviousValue(value)

declare const usePreviousValue: <T>(value: T) => T | undefined;

value

The value on the current render.