简体   繁体   English

如何使用 Typescript 解决这个 formik onSubmit 错误?

[英]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.

相关问题 我如何解决这个 TypeError: props.onSubmit is not a function? - How do I solve this TypeError: props.onSubmit is not a function? 如果发生错误,如何禁用表单onsubmit? - How do I disable form onsubmit if there is an error? 如何在 onSubmit 处理程序上使用 formik 的 setSubmitting() 方法? - How can I use formik's setSubmitting() method on onSubmit handler? 如何将 Formik 的 onSubmit 放在另一个组件上? - How can I put Formik's onSubmit on another component? 如何解决以下节点 Typescript 错误 - How Do solve the following Node Typescript Error 如何在 Formik 的“onSubmit”函数中正确地“useState”? - How to 'useState' correctly in Formik 'onSubmit' function? 如何解决 typescript 错误在“对象”类型上找不到带有“字符串”类型参数的索引签名 - How do i solve typescript error No index signature with a parameter of type 'string' was found on type 'Object' 我如何解决我的Typescript版本中的错误“找不到模块&#39;jquery&#39;” - How do i solve error “cannot find module 'jquery'” in my Typescript build 如何在PHP中插入onsubmit - How do I insert onsubmit in PHP 如何更改搜索栏中的 onSubmit 路线? - How do I change route onSubmit in a searchbar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM