Skip to content

Commit

Permalink
fix: callback will not be executed until it is a function (#3511)
Browse files Browse the repository at this point in the history
* fix: callback will not be executed until it is a function

* test: add test for callback is undefined

* style: move import dependent location
  • Loading branch information
hchlq authored Nov 2, 2022
1 parent f347a7c commit 0c96914
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/core/src/__tests__/effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/effects/onFormEffects.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isFn } from '@formily/shared'
import { autorun, batch } from '@formily/reactive'
import { Form } from '../models'
import { LifeCycleTypes } from '../types'
Expand Down Expand Up @@ -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(() => {
Expand Down

0 comments on commit 0c96914

Please sign in to comment.