简体   繁体   English

如何修复未定义类型的错误参数不可分配给'字符串或()=>字符串类型的参数使用typescript并做出反应?

[英]How to fix error argument of type undefined is not assignable to parameter of type 'string or () => string usng typescript and react?

i am getting error error argument of type undefined is not assignable to parameter of type 'string or () => string usng typescript and react.我收到未定义类型的错误错误参数不可分配给'字符串或()=>字符串类型的参数使用 typescript 并做出反应。

below is my code,下面是我的代码,

export const useSomething =()=> {
    const [itemId, setItemId] = React.useState<string>(
        undefined //getting error here
    );
    const toggle = (itemId: string) => {
        setItemId(itemId);
        return {toggle, itemId};

    }
 }


 function  Parent() {
     const {itemId, toggle} = useSomething();
     return (
        <Child anotherId={anotherId} itemId={itemId} toggle={toggle}/>         
     );
 }


 function Child({itemId, anotherId, toggle) {
    return (
        <Button onClick={() => toggle(anotherId || '')/>
    );
 }

i am not sure what is causing this error.could someone help me with this.我不确定是什么导致了这个错误。有人可以帮我解决这个问题。 thanks.谢谢。

You're setting the initial value of the state member to undefined , which isn't a string.您将 state 成员的初始值设置为undefined ,这不是字符串。 So to fix it, you have to either:因此,要修复它,您必须:

  1. Initialize the state member with a string, instead of undefined ;用字符串而不是undefined初始化 state 成员;

     const [itemId, setItemId] = React.useState<string>( "" // <=== or whatever string makes sense as the initial value );

    or或者

  2. Change the type of the state member to string | undefined将 state 成员的类型更改为string | undefined string | undefined (a union type ), which allows the member to be a string or be undefined : string | undefined联合类型),它允许成员是字符串undefined

     const [itemId, setItemId] = React.useState<string | undefined>( undefined // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^ );

Side note: If you're initializing to undefined , you can just leave the argument off entirely: = useState<string|undefined>() .旁注:如果您要初始化为undefined ,则可以完全关闭该参数: = useState<string|undefined>()

Since you initialize the state with undefined , the type of the state is not string but rather string | undefined由于您使用undefined初始化 state ,因此 state 的类型不是string而是string | undefined string | undefined (this is a union type, meaning the value can either be string or undefined ) string | undefined (这是一个联合类型,这意味着该值可以是stringundefined

export const useSomething =()=> {
    const [itemId, setItemId] = React.useState<string | undefined>(
        undefined //getting error here
    );
    const toggle = (itemId: string) => {
        setItemId(itemId);
        return {toggle, itemId};

    }
 }

This may lead to errors in other places (where you expect a string, but not undefined is also an option but you should fix those to handle undefined这可能会在其他地方导致错误(您期望一个字符串,但不是undefined也是一种选择,但您应该修复那些以处理undefined

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 typescript 修复字符串类型的错误参数或未定义的参数不可分配给字符串类型的参数并做出反应? - How to fix the error argument of type string or undefined is not assignable to parameter of type string using typescript and react? 如何修复 React TypeScript 错误:'""' 类型的参数不可分配给 'SetStateAction 类型的参数<undefined> '</undefined> - How to fix React TypeScript error: Argument of type '""' is not assignable to parameter of type 'SetStateAction<undefined>' React,typescript - 'string | 类型的参数 null' 不能分配给“字符串”类型的参数 - React, typescript - Argument of type 'string | null' is not assignable to parameter of type 'string' React Typescript - 'string' 类型的参数不可分配给 useState 中的类型参数 - React Typescript - Argument of type 'string' is not assignable to parameter of type within useState 如何使用react和typescript修复错误“类型参数(打开:任何)=&gt;布尔值不能分配给布尔类型的参数”? - How to fix error "argument of type (open: any) => boolean is not assignable to parameter of type boolean" using react and typescript? TS2345:“字符串”类型的参数 | undefined&#39; 不能分配给类型为 &#39;string&#39; 的参数。 “未定义”类型不能分配给“字符串”类型 - TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string' 'string | 类型的参数 undefined' 不能分配给'string' 类型的参数。 类型“未定义”不可分配给类型“字符串” - Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string' TypeScript - “字符串”类型的参数不可分配给“字符串”类型的参数 - TypeScript - Argument of type 'String' is not assignable to parameter of type 'string' Typescript:'(i: string[]) =&gt; any[]' 类型的参数不可分配给'string[]' 类型的参数 - Typescript: Argument of type '(i: string[]) => any[]' is not assignable to parameter of type 'string[]' 类型错误:类型为“{ projectId: string | 不明确的; }' 不可分配给 'SCL| 类型的参数防雷器 | 不明确的' - Type error: Argument of type '{ projectId: string | undefined; }' is not assignable to parameter of type 'SCL| SPD | undefined'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM