[英]How do I solve this formik onSubmit error with Typescript?
for some reason I'm getting an error on onSubmit prop of Formik:出于某种原因,我在 Formik 的 onSubmit 道具上遇到错误:
Type '(values: { email: string; password: string; }) => { type: string;
类型'(值:{电子邮件:字符串;密码:字符串;})=> {类型:字符串; payload: { email: string |
有效载荷:{电子邮件:字符串| null;
无效的; password: string |
密码:字符串 | null;
无效的; };
}; }' is not assignable to type '((values: { email: string; password: string; }, formikHelpers: FormikHelpers<{ email: string; password: string; }>) => void | Promise) & ((values: { email: string; password: string; }) => { ...; })'.
}' 不能分配给类型 '((values: { email: string; password: string; }, formikHelpers: FormikHelpers<{ email: string; password: string; }>) => void | Promise) & ((values: { 电子邮件:字符串;密码:字符串;}) => { ...; })'。 Type '(values: { email: string; password: string; }) => { type: string;
类型'(值:{电子邮件:字符串;密码:字符串;})=> {类型:字符串; payload: { email: string |
有效载荷:{电子邮件:字符串| null;
无效的; password: string |
密码:字符串 | null;
无效的; };
}; }' is not assignable to type '(values: { email: string; password: string; }, formikHelpers: FormikHelpers<{ email: string; password: string; }>) => void |
}' 不可分配给类型 '(值:{ 电子邮件:字符串;密码:字符串;},formikHelpers:FormikHelpers<{ 电子邮件:字符串;密码:字符串;}>)=> void | Promise'.
承诺'。 Type '{ type: string;
类型'{类型:字符串; payload: { email: string |
有效载荷:{电子邮件:字符串| null;
无效的; password: string |
密码:字符串 | null;
无效的; };
}; }' is not assignable to type 'void |
}' 不可分配给类型 'void | Promise'.
承诺'。
These are the Formik props:这些是 Formik 道具:
<Formik
initialValues={{ email: '', password: ''}}
validationSchema = { LoginSchema }
validateOnChange={false}
onSubmit={ (values) => requestLogin(values.email, values.password)}
>
And this is requestLogin action:这是 requestLogin 动作:
export const requestLogin = (email: string, password: string) => ({
type: types.AUTH_USER,
payload: {
email: email || null,
password: password || null
}
}); });
Everything should be fine since requestLogin takes 2 parameters of type 'string' or am I missing something here?一切都应该没问题,因为 requestLogin 需要 2 个“字符串”类型的参数,还是我在这里遗漏了什么?
Change改变
onSubmit={ (values) => requestLogin(values.email, values.password) }
to至
onSubmit={ (values) => {requestLogin(values.email, values.password)} }
Your error:你的错误:
Type '{ type: string; payload: { email: string | null; password: string | null; }; }' is not assignable to type 'void | Promise'.
You're returning an object with type { type: string, ... }
, instead of the type void | Promise
您正在返回一个类型为
{ type: string, ... }
的对象,而不是类型void | Promise
void | Promise
(returning nothing), because arrow functions with no curly brackets automatically return the value inside it. void | Promise
(不返回任何内容),因为没有大括号的箭头函数会自动返回其中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.