fix: useEffect only on update

This commit is contained in:
chenos 2022-01-20 23:48:35 +08:00
parent b0b79b7d9d
commit f882ab42c5

View File

@ -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 <SchemaField {...others} schema={schema} />;
}