[英]How to access formState.errors when using FormProvider
I'm building an abstract "deeply-nested" form component that uses FormProvider
.我正在构建一个使用
FormProvider
的抽象“深度嵌套”表单组件。 I have error validation working at the field
level, but I can't figure out how to access the errors
object at the formState
level.我在
field
级别进行了错误验证,但我不知道如何在formState
级别访问errors
object。
The FormProvider
docs say that "FormProvider requires all useForm methods," and the example code goes FormProvider
文档说“FormProvider 需要所有 useForm 方法”,示例代码如下
export default function App() {
const methods = useForm();
const onSubmit = data => console.log(data);
return (
<FormProvider {...methods} > // pass all methods into the context
...
My (seemingly working) abstract form component looks like this:我的(看似有效的)抽象表单组件如下所示:
let renderCount = 0;
function FormDialog(props) {
const {
open, onClose, loading, headline, noEscape, onFormSubmit, content, actions
} = props;
renderCount++;
let validationSchema = YupObject().shape({});
// construct schema
content.forEach(item => {
validationSchema = validationSchema.concat(YupObject().shape({
[item.component.props.name]: item.validation
}));
});
const methods = useForm({
resolver: yupResolver(validationSchema)
});
return (
<Dialog
open={open}
onClose={onClose}
disableEscapeKeyDown={loading || noEscape}
>
<DialogTitle>{headline}</DialogTitle>
<Box>
Render Count: {renderCount}
</Box>
<FormProvider {...methods}>
<DialogContent>
<Box component="form"
sx={{
onSubmit: methods.handleSubmit(onFormSubmit)
}}
>
<FormContent content={content} />
</Box>
<HighlightedInformation>
</HighlightedInformation>
</DialogContent>
<DialogActions>
<FormActions actions={actions} />
</DialogActions>
</FormProvider>
</Dialog>
);
}
export default FormDialog;
Like that, it renders, displays "Render Count: 2" (because I'm in dev mode, therefore using React.StrictMode
) and nicely points out errors at the field
level.就像那样,它渲染,显示“渲染计数:2”(因为我处于开发模式,因此使用
React.StrictMode
)并很好地指出了field
级别的错误。 However, I can't figure out how to access the formState
errors
property.但是,我不知道如何访问
formState
errors
属性。
All of the examples I can find pull the formState
out during the initial useForm
call:在最初的
useForm
调用期间,我能找到的所有示例都将formState
拉出:
const {
register,
handleSubmit,
formState
} = useForm();
However, I need to pull all of the methods
out because of the FormProvider
.但是,由于
FormProvider
,我需要提取所有methods
。 It doesn't seem a good idea to explicitly restructure them all.明确地重组它们似乎不是一个好主意。 Various things I've tried:
我尝试过的各种事情:
useFormState():使用表单状态():
...
const methods = useForm({
resolver: yupResolver(validationSchema)
});
const { errors } = useFormState();
console.log('errors', errors);
...
explicitly pass control
显式传递
control
const { errors } = useFormState( methods.control );
console.log('errors', errors);
both result in TypeError: null is not an object (evaluating 'methods.control')
.两者都导致
TypeError: null is not an object (evaluating 'methods.control')
。
Destructure methods
to get to the object :获取 object 的解构
methods
:
const methods = useForm({
resolver: yupResolver(validationSchema)
});
const { formState: {errors} } = methods;
console.log('errors', errors);
Wrap in useEffect as described in the formState
docs :按照
formState
文档中的描述包装useEffect :
useEffect(() => {
console.log('errors:', methods.formState.errors);
},[methods.formState]);
Both of the latter two result in infinite re-renders and the browser hanging.后两者都导致无限重新渲染和浏览器挂起。
So, how do I get to formState
errors
when using FormProvider
and pulling out all of the methods
?那么,在使用
FormProvider
并拉出所有methods
时,如何处理formState
errors
?
I've changed a few things and it seems to work now: Codesandbox我改变了一些东西,现在似乎可以工作了: Codesandbox
Don't pass onSubmit
as sx
prop.不要将
onSubmit
作为sx
道具传递。 I think sx is supposed to only accept style props, not sure why it allows callbacks and etc.我认为 sx 应该只接受样式道具,不知道为什么它允许回调等。
Wrap form
element around submit button, otherwise you won't be able to submit it.将
form
元素包裹在提交按钮周围,否则您将无法提交。
ConnectForm
was doing nothing so I removed it too, but not sure if it has any effect on the bug. ConnectForm
什么也没做,所以我也删除了它,但不确定它是否对 bug 有任何影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.