From f882ab42c5ddb8394b0160f2f1e98b40e180ea14 Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 20 Jan 2022 23:48:35 +0800 Subject: [PATCH] fix: useEffect only on update --- .../schema-component/components/SchemaComponent.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/client/src/schema-component/components/SchemaComponent.tsx b/packages/client/src/schema-component/components/SchemaComponent.tsx index 98f19a4c3..8ab13b8c5 100644 --- a/packages/client/src/schema-component/components/SchemaComponent.tsx +++ b/packages/client/src/schema-component/components/SchemaComponent.tsx @@ -1,5 +1,5 @@ -import React, { useContext, useEffect, useMemo } from 'react'; -import { Schema, ISchemaFieldProps } from '@formily/react'; +import { ISchemaFieldProps, Schema } from '@formily/react'; +import React, { useContext, useEffect, useMemo, useRef } from 'react'; import { SchemaComponentContext } from '../context'; function toSchema(schema?: any) { @@ -21,11 +21,13 @@ export function SchemaComponent(props: ISchemaFieldProps) { const { schema: defaultSchema, ...others } = props; const { reset, SchemaField } = useContext(SchemaComponentContext); const schema = useMemo(() => toSchema(defaultSchema), []); - // @ts-ignore - // console.log('defaultSchema', JSON.stringify(schema.toJSON())); - // TODO + const isInitialMount = useRef(true); useEffect(() => { + if (isInitialMount.current) { + isInitialMount.current = false; + } else { reset(); + } }, [JSON.stringify(defaultSchema)]); return ; }