@utilityjs/use-previous-value

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

Usage no npm install needed!

<script type="module">
  import utilityjsUsePreviousValue from 'https://cdn.skypack.dev/@utilityjs/use-previous-value';
</script>

README

usePreviousValue

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.