[英]Way to inject custom props/methods to `handleSubmit`?
I'm using react hook form , and I'm looking for a way to inject custom props to the handleSubmit
method returned by the hook.我正在使用react hook form ,并且我正在寻找一种方法将自定义道具注入由钩子返回的handleSubmit
方法。 The reason I need to do this is my component acts as a Consumer
for a state library , and I want to update the state after submitting the form.我需要这样做的原因是我的组件充当状态库的Consumer
,我想在提交表单后更新状态。 I may also want to pass props to the method.我可能还想将道具传递给方法。
From looking at the API, it seems like this isn't possible.从 API 来看,这似乎是不可能的。 Any thoughts on a workaround or how to do this?关于解决方法或如何执行此操作的任何想法?
I don't use this library, but it seems that getValues
function returned from useForm
hook opens the way to synchronize Your component state with form data stored in the "react-hook-form":我不使用这个库,但似乎从useForm
钩子返回的getValues
函数打开了将组件状态与存储在“react-hook-form”中的表单数据同步的方法:
import React, { useMemo, useEffect, useState } from "react";
import { useForm } from "react-hook-form";
export default function App() {
const { register, getValues } = useForm();
const [ valuesState, setValuesState ] = useState();
const values = useMemo(() => getValues());
useEffect(() => setValuesState(values), [values]);
return (
<form>
[...]
</form>
);
}
why not just update the state during handleSubmit
?为什么不在handleSubmit
期间更新状态?
export default function App() {
const { register, getValues } = useForm();
const onSubmit = data => {
// do your state update here update(data)
}
return (
<form onSubmit={handleSubmit(onSubmit)}>
[...]
</form>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.