-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathuseTimeline.ts
37 lines (33 loc) · 1.04 KB
/
useTimeline.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { useRef } from 'react';
import usePrevious from './usePrevious';
import { MAX_ARRAY_INDEX } from './utils';
/**
* Records states of a value over time.
*
* @param value Props, state or any other calculated value.
* @param maxLength Maximum amount of states to store. Should be an integer greater than 1.
* @returns Results of state updates in chronological order.
*
* @example
* function Example() {
* const [count, setCount] = useState(0);
* const counts = useTimeline(count);
* // ...
* return `Now: ${count}, history: ${counts}`;
* }
*/
export default function useTimeline<T>(
value: T,
maxLength: number = MAX_ARRAY_INDEX,
): ReadonlyArray<T> {
const valuesRef = useRef<T[]>([]);
const prevValue = usePrevious(value);
if (!Object.is(value, prevValue)) {
// Use immutable refs to behave like state variables
valuesRef.current = [...valuesRef.current, value];
}
if (valuesRef.current.length > maxLength) {
valuesRef.current.splice(0, valuesRef.current.length - maxLength);
}
return valuesRef.current;
}