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β
Argument | Type | Description |
---|---|---|
value | any | The variable whose previous value should be stored |
Gifβ
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.