react自定义hooks浅替换hoc实现预处理

First Post:

Last Update:

自定义hooks:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { useLocation } from 'react-router';
import { MetaForm } from '../metaRender/interface';
import { useEffect, useState } from 'react';
import { getCommonHints, getMetaData, getPageName } from '@/actions';
import { ErrorBlock, NoticeBar } from 'antd-mobile';
import React from 'react';
import Spin from '../spin';
import _ from 'lodash';

export type DocProps = {
meta: {
defaultValue: any;
metaData: MetaForm;
};
pageName: any;
warningTip: any;
sourceProps: any;
};

const useDoc = () => {
const location = useLocation();
const [loading, setLoading] = useState<boolean>(true);
const [props, setProps] = useState<DocProps>({
meta: {
metaData: {},
defaultValue: {},
},
sourceProps: {},
} as DocProps);

useEffect(() => {
init();
}, []);

const init = async () => {
const { state } = location;
const { id, billTypeCode = '', templateCode = '', orgId } = state;
const pkey = `${billTypeCode}${templateCode}`;
try {
const metaData = await getMetaData({
keyId: id,
pkey,
orgId,
});
const pageName = await getPageName({ templateCode: pkey });
const commonHints = id && (await getCommonHints(id, billTypeCode)).result;
setProps({
meta: {
defaultValue: metaData.attribute?.defaultValue || {},
metaData: metaData.result,
},
pageName: pageName.result,
warningTip: commonHints && <NoticeBar content={commonHints.result} color='alert' />,
sourceProps: state,
});
setLoading(false);
} catch (error) {
setLoading(false);
}
};

let loadNode: any = null;
if (loading) {
loadNode = <Spin spinning={loading} loadText='元数据加载中...'></Spin>;
} else if (_.isEmpty(props)) {
loadNode = <ErrorBlock title='元数据不存在或者配置错误' description='请检查元数据配置' fullPage />;
} else {
loadNode = null;
}

return { ...props, loadNode };
};

export default useDoc;

使用:

tips: 要确保所有的return操作在useEffect等hooks之后,否则会报错: 两次渲染间加载了额外的hooks

1
2
3
4
5
6
7
8
9
10
11
const Index: React.FC<any> = () => {
const { loadNode, ...props } = useDoc();

if (loadNode) {
return loadNode;
}

return <div>
组件内容
</div>
}