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 { ISchemaFieldProps, Schema } from '@formily/react';
import { Schema, ISchemaFieldProps } from '@formily/react'; import React, { useContext, useEffect, useMemo, useRef } from 'react';
import { SchemaComponentContext } from '../context'; import { SchemaComponentContext } from '../context';
function toSchema(schema?: any) { function toSchema(schema?: any) {
@ -21,11 +21,13 @@ export function SchemaComponent(props: ISchemaFieldProps) {
const { schema: defaultSchema, ...others } = props; const { schema: defaultSchema, ...others } = props;
const { reset, SchemaField } = useContext(SchemaComponentContext); const { reset, SchemaField } = useContext(SchemaComponentContext);
const schema = useMemo(() => toSchema(defaultSchema), []); const schema = useMemo(() => toSchema(defaultSchema), []);
// @ts-ignore const isInitialMount = useRef(true);
// console.log('defaultSchema', JSON.stringify(schema.toJSON()));
// TODO
useEffect(() => { useEffect(() => {
if (isInitialMount.current) {
isInitialMount.current = false;
} else {
reset(); reset();
}
}, [JSON.stringify(defaultSchema)]); }, [JSON.stringify(defaultSchema)]);
return <SchemaField {...others} schema={schema} />; return <SchemaField {...others} schema={schema} />;
} }