无法调用 Formik handleSubmit 内部的反应挂钩
Unable to call react hook inside of Formik handleSubmit
我无法在 Formik 的 handleSubmit
函数中调用 useDispatch
。单击 submit
按钮时,我想将提交的表单值发送到 redux 存储。我该怎么做?
这是错误 - An unhandled error was caught from submitForm(), [Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
这是我的代码片段
const handleSubmit = values => {
const dispatch = useDispatch();
console.log(dispatch)
dispatch({
type: "ADD_TO_PROFILE",
payload: {...values}
})
}
<Formik initialValues={{'dob': ''}}
onSubmit={handleSubmit}
>
({handleChange, handleBlur, setFieldValue, handleSubmit, values, errors }) => (
<Button onPress={handleSubmit} />
)}
</Formik>
像这样将 const dispatch = useDispatch();
移到 handleSumbit
之外:
const dispatch = useDispatch();
const handleSubmit = values => {
console.log(dispatch)
dispatch({
type: "ADD_TO_PROFILE",
payload: {...values}
})
}
<Formik initialValues={{'dob': ''}}
onSubmit={handleSubmit}
>
({handleChange, handleBlur, setFieldValue, handleSubmit, values, errors }) => (
<Button onPress={handleSubmit} />
)}
</Formik>
我无法在 Formik 的 handleSubmit
函数中调用 useDispatch
。单击 submit
按钮时,我想将提交的表单值发送到 redux 存储。我该怎么做?
这是错误 - An unhandled error was caught from submitForm(), [Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
这是我的代码片段
const handleSubmit = values => {
const dispatch = useDispatch();
console.log(dispatch)
dispatch({
type: "ADD_TO_PROFILE",
payload: {...values}
})
}
<Formik initialValues={{'dob': ''}}
onSubmit={handleSubmit}
>
({handleChange, handleBlur, setFieldValue, handleSubmit, values, errors }) => (
<Button onPress={handleSubmit} />
)}
</Formik>
像这样将 const dispatch = useDispatch();
移到 handleSumbit
之外:
const dispatch = useDispatch();
const handleSubmit = values => {
console.log(dispatch)
dispatch({
type: "ADD_TO_PROFILE",
payload: {...values}
})
}
<Formik initialValues={{'dob': ''}}
onSubmit={handleSubmit}
>
({handleChange, handleBlur, setFieldValue, handleSubmit, values, errors }) => (
<Button onPress={handleSubmit} />
)}
</Formik>