Skip to content

Commit 5207021

Browse files
authored
fix(react): fix field unmounted but can not update right model (#3994)
* fix(react): fix field unmounted but can not update right model * fix: fix workflow * fix: fix workflow * fix: fix workflow * fix: fix workflow * fix: fix ci
1 parent 421073e commit 5207021

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

.github/workflows/ci.yml

+3-7
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,14 @@ on:
1010

1111
jobs:
1212
build:
13-
runs-on: ${{ matrix.os }}
13+
runs-on: ubuntu-latest
1414
if: contains(github.event.head_commit.message, 'chore(versions)') == false
15-
strategy:
16-
matrix:
17-
node_version: 16
18-
os: [ubuntu-latest]
1915
steps:
2016
- uses: actions/checkout@v1
21-
- name: Use Node.js ${{ matrix.node_version }}
17+
- name: Use Node.js
2218
uses: actions/setup-node@v1
2319
with:
24-
node-version: ${{ matrix.node_version }}
20+
node-version: 16
2521

2622
- run: yarn -v
2723
- run: yarn --ignore-engines

packages/core/src/__tests__/form.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1713,3 +1713,11 @@ test('form initial values ref should not changed with setInitialValues', () => {
17131713
})
17141714
expect(form.initialValues === values).toBeTruthy()
17151715
})
1716+
1717+
test('form query undefined query should not throw error', () => {
1718+
const form = attach(createForm())
1719+
1720+
;(form.fields as any)['a'] = undefined
1721+
expect(() => form.query('*').take()).not.toThrowError()
1722+
expect(Object.keys(form.fields)).toEqual([])
1723+
})

packages/react/src/components/Field.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React from 'react'
1+
import React, { useEffect } from 'react'
22
import { useField, useForm } from '../hooks'
3-
import { useAttach } from '../hooks/useAttach'
43
import { ReactiveField } from './ReactiveField'
54
import { FieldContext } from '../shared'
65
import { JSXComponent, IFieldProps } from '../types'
@@ -10,9 +9,13 @@ export const Field = <D extends JSXComponent, C extends JSXComponent>(
109
) => {
1110
const form = useForm()
1211
const parent = useField()
13-
const field = useAttach(
14-
form.createField({ basePath: parent?.address, ...props })
15-
)
12+
const field = form.createField({ basePath: parent?.address, ...props })
13+
useEffect(() => {
14+
field?.onMount()
15+
return () => {
16+
field?.onUnmount()
17+
}
18+
}, [field])
1619
return (
1720
<FieldContext.Provider value={field}>
1821
<ReactiveField field={field}>{props.children}</ReactiveField>

0 commit comments

Comments
 (0)