Skip to main content
Version: 5.6.1

usePrevious

About​

Access the previous value of a variable with this React hook


Installation​

npm install --save rooks

Importing the hook​

import { usePrevious } from "rooks";

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

Codesandbox Example​

Basic Usage​

Join Bhargav's discord server​

You can click on the floating discord icon at the bottom right of the screen and talk to us in our server.