[英]React hook form returning blank object on submit
Here's my simplified problem:这是我的简化问题:
https://codesandbox.io/s/busy-fire-mm91r?file=/src/FormWrapper.tsx https://codesandbox.io/s/busy-fire-mm91r?file=/src/FormWrapper.tsx
And the code:和代码:
export const FormItemWrapper = ({ children }) => {
return <FormItem>{children}</FormItem>;
};
export const FormWrapper = ({ children }) => {
const { handleSubmit } = useForm();
const onSubmit = (data) => {
console.log(data); // logs { }
};
return <Form onFinish={handleSubmit(onSubmit)}>{children}</Form>;
};
export default function App() {
const { control, watch } = useForm();
console.log(watch()); // logs { }
return (
<FormWrapper>
<FormItemWrapper>
<Controller
control={control}
name="tmp"
render={({ field }) => <Input {...field} />}
/>
</FormItemWrapper>
<FormItemWrapper>
<Button htmlType="submit">Save</Button>
</FormItemWrapper>
</FormWrapper>
);
}
The problem:问题:
React-hook-form
doesn't seem to see the data I type in. I can get it using antd
, but can't with react-hook-form
. React-hook-form
似乎没有看到我输入的数据。我可以使用antd
获取它,但不能使用react-hook-form
。 Why?为什么? What am I missing?
我错过了什么?
watch()
logs only once, and it logs { }
. watch()
只记录一次,它记录{ }
。 onSubmit
logs { }
onSubmit
日志{ }
You have created two different form instances with useForm call.您已经使用 useForm 调用创建了两个不同的表单实例。 If you want to get current form context inside Controller you should use useFormContext and wrap your form in FormProvider.
如果您想在 Controller 中获取当前表单上下文,您应该使用 useFormContext 并将您的表单包装在 FormProvider 中。
https://codesandbox.io/s/reverent-star-4vtpo https://codesandbox.io/s/reverent-star-4vtpo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.