2022-04-14 20:46:57 +08:00
|
|
|
|
---
|
|
|
|
|
order: 3.3
|
|
|
|
|
version: 4.20.0
|
|
|
|
|
title:
|
|
|
|
|
zh-CN: 字段监听 Hooks
|
|
|
|
|
en-US: Watch Hooks
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
2022-07-11 14:40:35 +08:00
|
|
|
|
`useWatch` 允许你监听字段变化,同时仅当该字段变化时重新渲染。API 文档请[查阅此处](#Form.useWatch)。
|
2022-04-14 20:46:57 +08:00
|
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
2022-04-15 15:51:09 +08:00
|
|
|
|
`useWatch` helps watch the field change and only re-render for the value change. [API Ref](#Form.useWatch).
|
2022-04-14 20:46:57 +08:00
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
import React from 'react';
|
2022-04-15 15:51:09 +08:00
|
|
|
|
import { Form, Input, InputNumber, Typography } from 'antd';
|
2022-04-14 20:46:57 +08:00
|
|
|
|
|
|
|
|
|
const Demo = () => {
|
2022-04-18 10:37:12 +08:00
|
|
|
|
const [form] = Form.useForm<{ name: string; age: number }>();
|
2022-04-15 15:51:09 +08:00
|
|
|
|
const nameValue = Form.useWatch('name', form);
|
2022-04-14 20:46:57 +08:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Form form={form} layout="vertical" autoComplete="off">
|
2022-04-15 15:51:09 +08:00
|
|
|
|
<Form.Item name="name" label="Name (Watch to trigger rerender)">
|
2022-04-14 20:46:57 +08:00
|
|
|
|
<Input />
|
|
|
|
|
</Form.Item>
|
2022-04-15 15:51:09 +08:00
|
|
|
|
<Form.Item name="age" label="Age (Not Watch)">
|
|
|
|
|
<InputNumber />
|
2022-04-14 20:46:57 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
</Form>
|
|
|
|
|
|
|
|
|
|
<Typography>
|
2022-04-15 15:51:09 +08:00
|
|
|
|
<pre>Name Value: {nameValue}</pre>
|
2022-04-14 20:46:57 +08:00
|
|
|
|
</Typography>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Demo;
|
|
|
|
|
```
|