简体   繁体   English

如何使用 react 和 typescript 修复 boolean 类型或未定义的错误参数?

[英]How to fix the error argument of type boolean or undefined using react and typescript?

i am getting the error我收到错误

"argument of type boolean or undefined isnt assignable to parameter of type boolean. type undefined is not assignable to type boolean" “boolean 类型的参数或未定义的参数不可分配给 boolean 类型的参数。未定义的类型不可分配给布尔类型”

when i am passing prop from child to a usehook like below,当我将道具从孩子传递到下面的使用挂钩时,

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

function Parent() {
    const [isDialogOpen, setDialogOpen] = React.useState(false);
    return (
        <Wrapper>
            <DialogComponent isDialogOpen={isDialogOpen}                     setDailogOpen={setDialogOpen}/>
         </Wrapper>
    );
}

interface ChildComponentProps {
    isDialogOpen: boolean,
    setIsDialogOpen: any,
}

function ChildComponent ({isDialogOpen, setIsDialogOpen} : ChildComponentProps) {
    const count= 10;
    useSomehook(isDialogOpen, count); //error here
    return (
        <Wrapper>
            <div>somedivs</div>
            {isDialogOpen && <Overlay/>}
        </Wrapper>
    );
}

i have useSomehook defined in other file like below,我在其他文件中定义了 useSomehook,如下所示,

export function useSomehook(enabled: boolean, count:number) {
    React.useEffect(() => {
        if (enabled) {
            trigger(count);
        }
    }, [enabled, count, trigger]);
}

I am new to using typescript.我是使用 typescript 的新手。 i am not sure how to fix this error.我不知道如何解决这个错误。 could someone help me with this.有人可以帮我解决这个问题。 thanks.谢谢。

You have a useState using a boolean.您有一个使用 boolean 的 useState。 In typescript you have to be sure that whatever variable you pass as a boolean in this case can't be undefined.在 typescript 中,您必须确保在这种情况下作为 boolean 传递的任何变量都不能未定义。

There are multiple solutions, but a quick fix would be to fix you component like this.有多种解决方案,但快速解决方法是像这样修复您的组件。

function ChildComponent ({isDialogOpen, setIsDialogOpen} : ChildComponentProps) {
    const count= 10;
    useSomehook(isDialogOpen !== undefined ? isDialogOpen : false, count); //error here
    return (
        <Wrapper>
            <div>somedivs</div>
            {isDialogOpen && <Overlay/>}
        </Wrapper>
    );
}

This makes sure that if isDialogOpen is not undefined, we will use that value, otherwise we use the boolean "false".这确保如果 isDialogOpen 不是未定义的,我们将使用该值,否则我们使用 boolean “false”。 You can edit this to your liking of course.当然,您可以根据自己的喜好对其进行编辑。

暂无
暂无

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

相关问题 如何使用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 修复错误“false 类型的参数不可分配给 (currentValue: boolean) =&gt; boolean”? - How to fix error “argument of type false is not assignable to (currentValue: boolean) => boolean” using react and typescript? 如何使用 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>' 如何修复未定义类型的错误参数不可分配给'字符串或()=&gt;字符串类型的参数使用typescript并做出反应? - How to fix error argument of type undefined is not assignable to parameter of type 'string or () => string usng typescript and react? 如何修复错误类型 Item[] | undefined 不能使用 react 和 typescript 分配给 Item[] 类型? - How to fix the error type Item[] | undefined is not assignable to Item[] type using react and typescript? 如何使用 react 和 typescript 修复错误 object 可能未定义? - How to fix the error object could be possibly undefined using react and typescript? 如何使用 react 和 typescript 修复错误 object 可能是 null 或未定义? - How to fix the error object is possibly null or undefined using react and typescript? 如何使用 react 和 typescript 修复错误 object 可能未定义? - How to fix the error object is possibly undefined using react and typescript? 如何使用 react 和 typescript 修复错误无法读取未定义的属性? - How to fix error cannot read property of undefined using react and typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM