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