useFreshState.js
1.21 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
import { useRef, useEffect } from 'react';
import raf from "rc-util/es/raf";
import useForceUpdate from '../_util/hooks/useForceUpdate'; // Note. Only for upload usage. Do not export to global util hooks
export default function useFreshState(defaultValue, propValue) {
var valueRef = useRef(defaultValue);
var forceUpdate = useForceUpdate();
var rafRef = useRef(); // Set value
function setValue(newValue) {
valueRef.current = newValue;
forceUpdate();
}
function cleanUp() {
raf.cancel(rafRef.current);
}
function rafSyncValue(newValue) {
cleanUp();
rafRef.current = raf(function () {
setValue(newValue);
});
} // Get value
function getValue() {
var displayValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
if (displayValue) {
return propValue || valueRef.current;
}
return valueRef.current;
} // Effect will always update in a next frame to avoid sync state overwrite current processing state
useEffect(function () {
if (propValue) {
rafSyncValue(propValue);
}
}, [propValue]); // Clean up
useEffect(function () {
return function () {
cleanUp();
};
}, []);
return [getValue, setValue];
}