UNPKG

678 BJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = usePrevious;
5
6var _react = require("react");
7
8/**
9 * Store the last of some value. Tracked via a `Ref` only updating it
10 * after the component renders.
11 *
12 * Helpful if you need to compare a prop value to it's previous value during render.
13 *
14 * ```ts
15 * function Component(props) {
16 * const lastProps = usePrevious(props)
17 *
18 * if (lastProps.foo !== props.foo)
19 * resetValueFromProps(props.foo)
20 * }
21 * ```
22 *
23 * @param value the value to track
24 */
25function usePrevious(value) {
26 var ref = (0, _react.useRef)(null);
27 (0, _react.useEffect)(function () {
28 ref.current = value;
29 });
30 return ref.current;
31}
\No newline at end of file