Skip to main content
Version: v3.6.0

@rooks/use-previous

Access the previous value of a variable with this React hook


Build Status

Installation

npm install --save @rooks/use-previous

Importing the hook

import usePrevious from "@rooks/use-previous";

Usage

function Demo() {
const myInput = useInput("hello world");
const previousValue = usePrevious(myInput.value);
return (
<div>
<div>
<input {...myInput} />
</div>
<p>
Current value is <b>{myInput.value}</b>
</p>
<p>
Previous value was <b>{previousValue || "-"}</b>
</p>
</div>
);
}

render(<Demo />);

Arguments

ArgumentTypeDescription
valueanyThe variable whose previous value should be stored

Gif

Image from Gyazo