mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-16 01:41:02 +08:00
b62e409ad5
* feat: Support useWatch * test: update test case * chore: update snapshot
45 lines
953 B
Markdown
45 lines
953 B
Markdown
---
|
|
order: 3.3
|
|
version: 4.20.0
|
|
title:
|
|
zh-CN: 字段监听 Hooks
|
|
en-US: Watch Hooks
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
`useWatch` 允许你监听字段变化,同时仅当改字段变化时重新渲染。
|
|
|
|
## en-US
|
|
|
|
`useWatch` helps watch the field change and only re-render for the value change.
|
|
|
|
```tsx
|
|
import React from 'react';
|
|
import { Form, Input, Typography } from 'antd';
|
|
|
|
const Demo = () => {
|
|
const [form] = Form.useForm<{ field1: string; field2: string }>();
|
|
const nameValue = Form.useWatch(['field1'], form);
|
|
|
|
return (
|
|
<>
|
|
<Form form={form} layout="vertical" autoComplete="off">
|
|
<Form.Item name="field1" label="Field 1 (Watch to trigger rerender)">
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.Item name="field2" label="Field 2 (Not Watch)">
|
|
<Input />
|
|
</Form.Item>
|
|
</Form>
|
|
|
|
<Typography>
|
|
<pre>Field 1 Value: {nameValue}</pre>
|
|
</Typography>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default Demo;
|
|
```
|