From 0c969140a37d88dd9fa11f2f8ca5f72cd7ca1580 Mon Sep 17 00:00:00 2001 From: huangcheng Date: Wed, 2 Nov 2022 12:15:56 +0800 Subject: [PATCH] fix: callback will not be executed until it is a function (#3511) * fix: callback will not be executed until it is a function * test: add test for callback is undefined * style: move import dependent location --- packages/core/src/__tests__/effects.spec.ts | 11 +++++++++++ packages/core/src/effects/onFormEffects.ts | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/core/src/__tests__/effects.spec.ts b/packages/core/src/__tests__/effects.spec.ts index c33b2fcbf95..8b6fedcdb2f 100644 --- a/packages/core/src/__tests__/effects.spec.ts +++ b/packages/core/src/__tests__/effects.spec.ts @@ -132,6 +132,17 @@ test('onFormReact', () => { form.setValues({ aa: 123 }) expect(react).toBeCalled() form.onUnmount() + + // will not throw error + const form2 = attach( + createForm({ + effects() { + onFormReact() + }, + }) + ) + + form2.onUnmount() }) test('onFormReset', async () => { diff --git a/packages/core/src/effects/onFormEffects.ts b/packages/core/src/effects/onFormEffects.ts index 1d01cd512f8..d65fbeb68c0 100644 --- a/packages/core/src/effects/onFormEffects.ts +++ b/packages/core/src/effects/onFormEffects.ts @@ -1,3 +1,4 @@ +import { isFn } from '@formily/shared' import { autorun, batch } from '@formily/reactive' import { Form } from '../models' import { LifeCycleTypes } from '../types' @@ -72,7 +73,7 @@ export function onFormReact(callback?: (form: Form) => void) { let dispose = null onFormInit((form) => { dispose = autorun(() => { - callback(form) + if (isFn(callback)) callback(form) }) }) onFormUnmount(() => {