动作调度时formik表单重置



在我分派操作后,有没有任何可能的方法可以立即重置表单?

类似于:

const formik = useFormik({
onSubmit: (values, {resetForm}) => {
dispatch(action.register(values)) //some action creator with axios.post request
if (isRegistred) { resetForm() }  //isRegistred - value from Selector which changes on dispatch to true
}
})
const register = (user) => (dispatch) => {
return axios
.post(`${API_URL}/user/create/`, {
first_name: user.firstName,
second_name: user.secondName,
date_birth: user.dateBirth,
phone_number: user.phoneNumber,
gender: user.gender,
email: user.email,
city: user.city,
username: user.username,
password1: user.password1,
password2: user.password2
})
.then((response) => {
dispatch({ type: '@USER/register-success', isRegistred: response.data?.success })
})
.catch((error) => {
dispatch({ type: '@USER/register-error', error: error?.response?.data?.errors })
})
}
export const action = { register }

我知道,这有点傻,因为它马上就改变了价值。也许还有其他方法?

如果有人愿意,我只需将onSubmit formik中的resetForm传递给动作创建者。

const formik = useFormik({
initialValues,
validationSchema,
onSubmit: (values, { resetForm }) => {
dispatch(userActions.register(values, { resetForm }))
},
validateOnChange: false,
validateOnBlur: true
})
const register = (user, { resetForm }) => (dispatch) => {
return axios
.post(`${API_URL}/user/create/`, {
first_name: user.firstName,
second_name: user.secondName,
date_birth: user.dateBirth,
phone_number: user.phoneNumber,
gender: user.gender,
email: user.email,
city: user.city,
username: user.username,
password1: user.password1,
password2: user.password2
})
.then((response) => {
dispatch({ type: '@USER/register-request' })
if (response.data?.success) {
dispatch({ type: '@USER/register-success', isRegistred: response.data?.success })
resetForm()
}
})
.catch((error) => {
dispatch({ type: '@USER/register-error', error: error?.response?.data?.errors })
})
}

相关内容

  • 没有找到相关文章

最新更新