diff --git a/src/useWatch.ts b/src/useWatch.ts index 127c04d5..e3dc8ac0 100644 --- a/src/useWatch.ts +++ b/src/useWatch.ts @@ -140,6 +140,9 @@ function useWatch( const { registerWatch } = getInternalHooks(HOOK_MARK); const getWatchValue = (values: any, allValues: any) => { + if (dependencies === undefined) { + return undefined; + } const watchValue = options.preserve ? allValues : values; return typeof dependencies === 'function' ? dependencies(watchValue) diff --git a/tests/useWatch.test.tsx b/tests/useWatch.test.tsx index 0c08ab19..f6cfb695 100644 --- a/tests/useWatch.test.tsx +++ b/tests/useWatch.test.tsx @@ -55,6 +55,34 @@ describe('useWatch', () => { expect(container.querySelector('.values')?.textContent).toEqual('bamboo'); }); + it('undefined', async () => { + const Demo = ({ name }: { name?: string }) => { + const [form] = Form.useForm(); + const nameValue = Form.useWatch(name, form); + return ( +
+
+ + + +
+
{nameValue}
+
+ ); + }; + + const { container, rerender } = render(); + await act(async () => { + await timeout(); + }); + expect(container.querySelector('.values')?.textContent).toEqual(''); + rerender(); + await act(async () => { + await timeout(); + }); + expect(container.querySelector('.values')?.textContent).toEqual(''); + }); + it('change value with form api', async () => { const staticForm = React.createRef(); const Demo: React.FC = () => {