清除formik错误并形成数据 - 反应本机
我正在使用 Formik,想知道在离开屏幕时如何清除错误和表单值。
例如,用户尝试提交没有值的表单,并显示错误:
当用户导航到不同的屏幕然后返回时,这些错误仍然存在。有办法清除这些吗?作为示例,我可以在 useEffect
挂钩中访问 Formik 方法吗?
这是我到目前为止的实现:
export const SignIn = ({route, navigation}) => {
const formValidationSchema = Yup.object().shape({
signInEmail: Yup.string()
.required('Email address is required')
.email('Please provide a valid email address')
.label('Email'),
signInPassword: Yup.string()
.required('Password is required')
.label('Password'),
});
const initialFormValues = {
signInEmail: '',
signInPassword: '',
};
return (
<Formik
initialValues={initialFormValues}
validationSchema={formValidationSchema}
onSubmit={(values, formikActions) => {
handleFormSubmit(values);
}}>
{({handleChange, handleSubmit, errors}) => (
<>
<SignInForm
messages={errors}
navigation={navigation}
handleFormSubmit={handleSubmit}
/>
</>
)}
</Formik>
)
}
I'm using Formik and was wondering how I go about clearing the errors and form values when leaving a screen.
For example, a user tries to submit the form with no values and the errors are displayed:
When the user then navigates to a different screen and then comes back those errors are still present. Is there a way to clear these? Can I access Formik methods within a useEffect
hook as an example?
This is my implementation so far:
export const SignIn = ({route, navigation}) => {
const formValidationSchema = Yup.object().shape({
signInEmail: Yup.string()
.required('Email address is required')
.email('Please provide a valid email address')
.label('Email'),
signInPassword: Yup.string()
.required('Password is required')
.label('Password'),
});
const initialFormValues = {
signInEmail: '',
signInPassword: '',
};
return (
<Formik
initialValues={initialFormValues}
validationSchema={formValidationSchema}
onSubmit={(values, formikActions) => {
handleFormSubmit(values);
}}>
{({handleChange, handleSubmit, errors}) => (
<>
<SignInForm
messages={errors}
navigation={navigation}
handleFormSubmit={handleSubmit}
/>
</>
)}
</Formik>
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里的问题在于,如果我们导航到其他屏幕,并且仅在屏幕上设置了Formik表单的初始值,则不会卸下屏幕。
我已经在一个字段中创建了一个最小的示例,只要将
提交
按钮执行到stack.navigator
中的其他屏幕时,我就会导航。请注意,如果您的表单字段中存在错误,通常不会触发
onsubmit
函数。但是,由于我想提供快速测试,因此我通过直接调用该功能来手动导航。如果我们通过按Navigator的
onback
按钮来浏览,则表单字段将重置为默认值,所有错误将自动重置。我们可以使用
formik InnerRef Prop
和focus focus侦听器
手工触发此触发。为了进行测试,您应该执行以下操作。
总体而言,这将与每个导航器一起使用,例如在标签器中更改选项卡。Navigator,它将重置错误和字段的内容。
关键部分由以下代码段提供。
完整的代码如下提供。
The problem here is that a screen does not get unmounted if we navigate to a different screen and the initial values of a Formik form will only be set on screen mount.
I have created a minimal example with one field and I navigate whenever the
submit
button is executed to a different Screen in aStack.Navigator
.Notice that the
onSubmit
function is usually not fired if there are errors in your form fields. However, since I wanted to provide a quick test, I navigate by hand by calling the function directly.If we navigate back by pressing the
onBack
button of the navigator, the form fields will be reseted to the default values and all errors will be reseted automatically.We can trigger this by hand using the
Formik innerRef prop
and afocus listener
.For testing this, you should do the following.
In principal, this will work with every navigator, e.g. changing a Tab in a Tab.Navigator and it will reset both, the errors and the field's content.
The key part is given by the following code snippet.
The full code is given as follows.
通过React导航的UseFocuseffect挂钩,这应该是可能的。它首次触发,每次屏幕焦点。
This should be possible with the useFocusEffect hook from react navigation. It gets triggered on first time and every time the screen comes in focus.
谢谢Maximilian Dietel。
这对我有用:
Thank you Maximilian Dietel.
This worked for me: