@norejs/react-utils 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/ErrorBoundary.tsx +45 -0
- package/hooks/useDebounce.ts +16 -0
- package/index.ts +0 -0
- package/package.json +11 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import React, { Component } from 'react';
|
|
2
|
+
|
|
3
|
+
export type IProps = {
|
|
4
|
+
fallback?: React.ReactNode;
|
|
5
|
+
onError?: (error: Error, errorInfo: React.ErrorInfo) => void;
|
|
6
|
+
children?: React.ReactNode;
|
|
7
|
+
};
|
|
8
|
+
type IState = {
|
|
9
|
+
hasError: boolean;
|
|
10
|
+
};
|
|
11
|
+
export default class ErrorBoundary extends Component<IProps, IState> {
|
|
12
|
+
state = {
|
|
13
|
+
hasError: false,
|
|
14
|
+
};
|
|
15
|
+
constructor(props) {
|
|
16
|
+
super(props);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static getDerivedStateFromError(error) {
|
|
20
|
+
// Update state so the next render will show the fallback UI.
|
|
21
|
+
return { hasError: true };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
componentDidCatch(error, errorInfo) {}
|
|
25
|
+
|
|
26
|
+
render() {
|
|
27
|
+
if (this.state.hasError) {
|
|
28
|
+
return this.props.fallback || null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return this.props.children;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const withErrorBoundary = (Component, fallback = null) => {
|
|
36
|
+
return class newComponent extends React.Component {
|
|
37
|
+
render() {
|
|
38
|
+
return (
|
|
39
|
+
<ErrorBoundary fallback={fallback}>
|
|
40
|
+
<Component {...(this.props || {})} />
|
|
41
|
+
</ErrorBoundary>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// import { useCallback, useRef, useMemo } from 'react';
|
|
2
|
+
// import { debounce } from '../modules/debounce';
|
|
3
|
+
// export function useDebounce(fn, state = [], delay = 500) {
|
|
4
|
+
// const cbRef = useRef(() => {});
|
|
5
|
+
// cbRef.current = useCallback(() => {
|
|
6
|
+
// fn();
|
|
7
|
+
// }, [fn, ...state]);
|
|
8
|
+
|
|
9
|
+
// return useMemo(() => {
|
|
10
|
+
// // 原来的 debounce 的 setTimeout 清楚
|
|
11
|
+
// return debounce(() => {
|
|
12
|
+
// // console.log(cbRef?.current)
|
|
13
|
+
// cbRef?.current?.();
|
|
14
|
+
// }, delay);
|
|
15
|
+
// }, []);
|
|
16
|
+
// }
|
package/index.ts
ADDED
|
File without changes
|