docs: add faq (#45016)

This commit is contained in:
二货爱吃白萝卜 2023-09-22 13:41:36 +08:00 committed by GitHub
parent dcfd1aea66
commit 7e1ecf9133
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -605,6 +605,22 @@ In most case, we always recommend to use Form `initialValues`. Use Item `initial
`getFieldsValue` returns collected field data by default, but the Form.Item node is not ready at the first render. You can get all field data by `getFieldsValue(true)`.
### Why some component not response with `setFieldsValue` to `undefined`?
`value` change from certain one to `undefined` in React means from controlled mode to uncontrolled mode. Thus it will not change display value but modified FormStore in fact. You can HOC to handle this:
```jsx
const MyInput = ({
// Force use controlled mode
value = '',
...rest
}) => <input value={value} {...rest} />;
<Form.Item name="my">
<MyInput />
</Form.Item>;
```
### Why does `onFieldsChange` trigger three times on change when field sets `rules`?
Validating is also part of the value updating. It pass follow steps:

View File

@ -604,6 +604,22 @@ validator(rule, value, callback) => {
`getFieldsValue` 默认返回收集的字段数据,而在初次渲染时 Form.Item 节点尚未渲染,因而无法收集到数据。你可以通过 `getFieldsValue(true)` 来获取所有字段数据。
### 为什么 `setFieldsValue` 设置字段为 `undefined` 时,有的组件不会重置为空?
在 React 中,`value` 从确定值改为 `undefined` 表示从受控变为非受控,因而不会重置展示值(但是 Form 中的值确实已经改变)。你可以通过 HOC 改变这一逻辑:
```jsx
const MyInput = ({
// 强制保持受控逻辑
value = '',
...rest
}) => <input value={value} {...rest} />;
<Form.Item name="my">
<MyInput />
</Form.Item>;
```
### 为什么字段设置 `rules` 后更改值 `onFieldsChange` 会触发三次?
字段除了本身的值变化外,校验也是其状态之一。因而在触发字段变化会经历以下几个阶段: