Skip to content

Commit 250a65a

Browse files
committed
feat: useWatch init object
1 parent 673536c commit 250a65a

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

docs/examples/useWatch-selector.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-shadow */
12
import React from 'react';
23
import Form, { Field } from 'rc-field-form';
34
import Input from './components/Input';
@@ -10,11 +11,15 @@ type FieldType = {
1011

1112
export default () => {
1213
const [form] = Form.useForm<FieldType>();
14+
const base = Form.useWatch(values => ({ newName: values.name }), form);
15+
console.log('base', base);
16+
1317
const values = Form.useWatch(
1418
values => ({ init: values.init, newName: values.name, newAge: values.age }),
1519
{ form, preserve: true },
1620
);
1721
console.log('values', values);
22+
1823
return (
1924
<>
2025
<Form form={form} initialValues={{ init: 'init', name: 'aaa' }}>

src/useWatch.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ function useWatch(
9292
const options = isFormInstance(_form) ? { form: _form } : _form;
9393
const form = options.form;
9494

95-
const [value, setValue] = useState<any>();
95+
const [value, setValue] = useState<any>(
96+
typeof dependencies === 'function' ? dependencies({}) : undefined,
97+
);
9698

9799
const valueStr = useMemo(() => stringify(value), [value]);
98100
const valueStrRef = useRef(valueStr);
@@ -117,8 +119,8 @@ function useWatch(
117119
// ============================= Update =============================
118120
const triggerUpdate = useEvent((values?: any, allValues?: any) => {
119121
const watchValue = options.preserve
120-
? (allValues ?? getFieldsValue(true))
121-
: (values ?? getFieldsValue());
122+
? allValues ?? getFieldsValue(true)
123+
: values ?? getFieldsValue();
122124

123125
const nextValue =
124126
typeof dependencies === 'function'

0 commit comments

Comments
 (0)