From 9a1794a932a4d1152d940b7e3c9e2e9980555136 Mon Sep 17 00:00:00 2001 From: Rairn <958414905@qq.com> Date: Tue, 14 Mar 2023 00:31:15 +0800 Subject: [PATCH] fix: avoid infinite loop --- .../collection-manager/hooks/useCollectionManager.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts b/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts index 2f2011341..29bd148f7 100644 --- a/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts +++ b/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts @@ -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; };