简体   繁体   English

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

[英]How to fix the error argument of type string or undefined is not assignable to parameter of type string using typescript and react?

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

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

    function List({items}: Props) { 
        const [activeindex, setActiveindex] = React.useState<string>();
        return (
            items.map((item: any) => {
                <AddButton setActiveIndex={setActiveIndex} itemId={item.id}/>
            }
        );
    }

    
    interface AddButtonProps {
        setActiveIndex: any;
        itemId?: string;
    }
    function AddButton ({setActiveIndex, itemId}: AddButtonProps){
        const {toggleDrawing} = useDrawing();
        const handleClick = (itemId) => {
            setActiveIndex(itemId);
            toggleDrawing(itemId); //error here
        }
        return (
            <Button onClick={handleClick}/>
        );
    );


    function useDrawing () {
        const [editableItemId, setEditableItemId] = React.useState<string>();
        const toggleDrawing = React.useCallback(async (itemId: string) => {
            if (isDrawing && editableItemId === itemId) {
                cancelDrawing();
            } else if (isDrawing) {
                cancelDrawing();
                setEditableItemId(itemId);
            } else {
                setEditableWorkAreaId(itemId);
            }
        },
            [isDrawingPolygon, editableItemId, cancelDrawing]
    );
}

I am not sure why i am getting this error.我不确定为什么会收到此错误。 could someone help me fix this.有人可以帮我解决这个问题。 thanks.谢谢。

try this way;)试试这个方法;)

const toggleDrawing = React.useCallback(async (itemId: string | undefined) => {
    if (isDrawing && editableItemId === itemId) {
        cancelDrawing();
    } else if (isDrawing) {
        cancelDrawing();
        setEditableItemId(itemId);
    } else {
        setEditableWorkAreaId(itemId);
    }
   },[])

You interface defines itemId?: string;你的接口定义了itemId?: string; and a string or undefined but the function call needs a string.和一个字符串或未定义,但 function 调用需要一个字符串。

Use either使用任一

if (itemId) {
  toggleDrawing(itemId);
}

or或者

toggleDrawing(itemId || '');

or或者

toggleDrawing(itemId as string);

暂无
暂无

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

相关问题 如何修复未定义类型的错误参数不可分配给'字符串或()=&gt;字符串类型的参数使用typescript并做出反应? - How to fix error argument of type undefined is not assignable to parameter of type 'string or () => string usng 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修复错误“类型参数(打开:任何)=&gt;布尔值不能分配给布尔类型的参数”? - How to fix error "argument of type (open: any) => boolean is not assignable to parameter of type boolean" using react and typescript? 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 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' 如何修复 typescript 错误类型“string”不可分配给类型“Ref”<inputref> | 不明确的'?</inputref> - How to fix typescript error Type 'string' is not assignable to type 'Ref<InputRef> | undefined'? 'string | 类型的参数 undefined' 不能分配给'string' 类型的参数。 类型“未定义”不可分配给类型“字符串” - Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string' 如何使用 react 和 typescript 修复 boolean 类型或未定义的错误参数? - How to fix the error argument of type boolean or undefined using react and typescript? TypeScript - “字符串”类型的参数不可分配给“字符串”类型的参数 - TypeScript - Argument of type 'String' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM