1 | import * as React from 'react';
|
2 | import useForceUpdate from '../../_util/hooks/useForceUpdate';
|
3 |
|
4 |
|
5 | export default function useCacheErrors(errors, changeTrigger, directly) {
|
6 | var cacheRef = React.useRef({
|
7 | errors: errors,
|
8 | visible: !!errors.length
|
9 | });
|
10 | var forceUpdate = useForceUpdate();
|
11 |
|
12 | var update = function update() {
|
13 | var prevVisible = cacheRef.current.visible;
|
14 | var newVisible = !!errors.length;
|
15 | var prevErrors = cacheRef.current.errors;
|
16 | cacheRef.current.errors = errors;
|
17 | cacheRef.current.visible = newVisible;
|
18 |
|
19 | if (prevVisible !== newVisible) {
|
20 | changeTrigger(newVisible);
|
21 | } else if (prevErrors.length !== errors.length || prevErrors.some(function (prevErr, index) {
|
22 | return prevErr !== errors[index];
|
23 | })) {
|
24 | forceUpdate();
|
25 | }
|
26 | };
|
27 |
|
28 | React.useEffect(function () {
|
29 | if (!directly) {
|
30 | var timeout = setTimeout(update, 10);
|
31 | return function () {
|
32 | return clearTimeout(timeout);
|
33 | };
|
34 | }
|
35 | }, [errors]);
|
36 |
|
37 | if (directly) {
|
38 | update();
|
39 | }
|
40 |
|
41 | return [cacheRef.current.visible, cacheRef.current.errors];
|
42 | } |
\ | No newline at end of file |