Version: Next

use-previous

About#

Access the previous value of a variable with this React hook


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

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.

Last updated on by imbhargav5