|
|
@ -354,9 +354,9 @@ export default () => { |
|
|
|
|
|
|
|
### Form.useWatch |
|
|
|
|
|
|
|
`type Form.useWatch = (namePath: NamePath, formInstance?: FormInstance): Value` |
|
|
|
`type Form.useWatch = (namePath: NamePath, formInstance?: FormInstance | WatchOptions): Value` |
|
|
|
|
|
|
|
Added in `4.20.0`. Watch the value of a field. You can use this to interactive with other hooks like `useSWR` to reduce develop cost: |
|
|
|
Watch the value of a field. You can use this to interact with other hooks like `useSWR` to reduce development costs: |
|
|
|
|
|
|
|
```tsx |
|
|
|
const Demo = () => { |
|
|
@ -375,6 +375,30 @@ const Demo = () => { |
|
|
|
}; |
|
|
|
``` |
|
|
|
|
|
|
|
If your component is wrapped by `Form.Item`, you can omit the second argument, `Form.useWatch` will find the nearest `FormInstance` automatically. |
|
|
|
|
|
|
|
By default `useWatch` only watches the registered field. If you want to watch the unregistered field, please use `preserve`: |
|
|
|
|
|
|
|
```tsx |
|
|
|
const Demo = () => { |
|
|
|
const [form] = Form.useForm(); |
|
|
|
|
|
|
|
const age = Form.useWatch('age', { form, preserve: true }); |
|
|
|
console.log(age); |
|
|
|
|
|
|
|
return ( |
|
|
|
<div> |
|
|
|
<Button onClick={() => form.setFieldValue('age', 2)}>Update</Button> |
|
|
|
<Form form={form}> |
|
|
|
<Form.Item name="name"> |
|
|
|
<Input /> |
|
|
|
</Form.Item> |
|
|
|
</Form> |
|
|
|
</div> |
|
|
|
); |
|
|
|
}; |
|
|
|
``` |
|
|
|
|
|
|
|
### Form.Item.useStatus |
|
|
|
|
|
|
|
`type Form.useFormItemStatus = (): { status: ValidateStatus | undefined }` |
|
|
@ -442,6 +466,13 @@ type Rule = RuleConfig | ((form: FormInstance) => RuleConfig); |
|
|
|
| warningOnly | Warning only. Not block form submit | boolean | 4.17.0 | |
|
|
|
| whitespace | Failed if only has whitespace, only work with `type: 'string'` rule | boolean | | |
|
|
|
|
|
|
|
#### WatchOptions |
|
|
|
|
|
|
|
| Name | Description | Type | Default | Version | |
|
|
|
| --- | --- | --- | --- | --- | |
|
|
|
| form | Form instance | FormInstance | Current form in context | 5.4.0 | |
|
|
|
| preserve | Whether to watch the field which has no matched `Form.Item` | boolean | false | 5.4.0 | |
|
|
|
|
|
|
|
## FAQ |
|
|
|
|
|
|
|
### Custom validator not working |
|
|
|