Skip to content

Commit

Permalink
fix(react): fix field unmounted but can not update right model (#3994)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
janryWang authored Oct 18, 2023
1 parent 421073e commit 5207021
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ on:

jobs:
build:
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
if: contains(github.event.head_commit.message, 'chore(versions)') == false
strategy:
matrix:
node_version: 16
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node_version }}
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node_version }}
node-version: 16

- run: yarn -v
- run: yarn --ignore-engines
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/__tests__/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1713,3 +1713,11 @@ test('form initial values ref should not changed with setInitialValues', () => {
})
expect(form.initialValues === values).toBeTruthy()
})

test('form query undefined query should not throw error', () => {
const form = attach(createForm())

;(form.fields as any)['a'] = undefined
expect(() => form.query('*').take()).not.toThrowError()
expect(Object.keys(form.fields)).toEqual([])
})
13 changes: 8 additions & 5 deletions packages/react/src/components/Field.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react'
import React, { useEffect } from 'react'
import { useField, useForm } from '../hooks'
import { useAttach } from '../hooks/useAttach'
import { ReactiveField } from './ReactiveField'
import { FieldContext } from '../shared'
import { JSXComponent, IFieldProps } from '../types'
Expand All @@ -10,9 +9,13 @@ export const Field = <D extends JSXComponent, C extends JSXComponent>(
) => {
const form = useForm()
const parent = useField()
const field = useAttach(
form.createField({ basePath: parent?.address, ...props })
)
const field = form.createField({ basePath: parent?.address, ...props })
useEffect(() => {
field?.onMount()
return () => {
field?.onUnmount()
}
}, [field])
return (
<FieldContext.Provider value={field}>
<ReactiveField field={field}>{props.children}</ReactiveField>
Expand Down

0 comments on commit 5207021

Please sign in to comment.