fix: avoid infinite loop

This commit is contained in:
Rairn 2023-03-14 00:31:15 +08:00
parent 976b094f96
commit 9a1794a932

View File

@ -1,5 +1,6 @@
import { clone } from '@formily/shared';
import { CascaderProps } from 'antd';
import _ from 'lodash';
import { reduce, unionBy, uniq, uniqBy } from 'lodash';
import { useContext } from 'react';
import { useCompile } from '../../schema-component';
@ -81,6 +82,9 @@ export const useCollectionManager = () => {
return collection?.fields || [];
};
// 缓存下面已经获取的 options防止无限循环
const cachedOptions = {};
const getCollectionFieldsOptions = (
collectionName: string,
type: string | string[] = 'string',
@ -92,6 +96,11 @@ export const useCollectionManager = () => {
association?: boolean | string[];
},
) => {
// 防止无限循环
if (cachedOptions[collectionName]) {
return _.cloneDeep(cachedOptions[collectionName]);
}
const { association = false } = opts || {};
if (typeof type === 'string') {
type = [type];
@ -122,6 +131,7 @@ export const useCollectionManager = () => {
// 过滤 map 产生为 null 的数据
.filter(Boolean);
cachedOptions[collectionName] = options;
return options;
};