简体   繁体   English

如何使用react和typescript修复错误“类型参数(打开:任何)=>布尔值不能分配给布尔类型的参数”?

[英]How to fix error "argument of type (open: any) => boolean is not assignable to parameter of type boolean" using react and typescript?

i want to fix the error "argument of type (open: any) => boolean is not assignable to parameter of type boolean" using react and typescript我想使用 react 和 typescript 修复错误“类型参数(打开:任何)=> 布尔值不可分配给布尔类型的参数”

i have a method toggleDialogVisibility defined like below我有一个方法 toggleDialogVisibility 定义如下

export const useDialog() {
    const {setDialogVisibility} = React.useContext(DialogContext);
    return (
        const toggleDialogVisibility = (toggleValue: boolean) => {
            setDialogVisible(toggleValue);
        };
    );
}

and i am calling it in other component like below,我在下面的其他组件中调用它,

function Parent() {
    const {toggleDialogVisibility} = useDialog();
    return (
        <div onClick={() => toggleDialogVisibility(open => !open)} /> //getting error here
    );
}

i am not sure how to what type to be passed here.我不知道如何在这里传递什么类型。 could someone help me with this.有人可以帮我解决这个问题吗? thanks.谢谢。

The type of param function toggleDialogVisibility is boolean.参数函数toggleDialogVisibility的类型是布尔值。 You should send a boolean instead a callback function.您应该发送一个布尔值而不是一个回调函数。

() => toggleDialogVisibility(!open)}

send the toggle value as well not just the function this solved it for me, in the parent component:在父组件中发送切换值,而不仅仅是为我解决它的函数:

 const [toggle, setToggle] = useState<boolean>(false);
   <EditForm setToggle={setToggle} open={toggle}/>

in the child compnent:在子组件中:

interface Props{
    open: boolean,
    setToggle: (toggle: boolean) => void
  }
  setToggle(!open);

暂无
暂无

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

相关问题 如何使用 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? 如何使用 react 和 typescript 修复 boolean 类型或未定义的错误参数? - How to fix the error argument of type boolean or undefined 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? TypeScript 错误:类型 &#39;() =&gt; boolean&#39; 不可分配给类型 &#39;boolean&#39; - TypeScript error: Type '() => boolean' is not assignable to type 'boolean' 如何修复 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? 出现错误:'() =&gt; () =&gt; boolean' 类型的参数不可分配给'EffectCallback' 类型的参数 - getting error : Argument of type '() => () => boolean' is not assignable to parameter of type 'EffectCallback' 无效类型不能分配给布尔类型(反应/打字稿) - Type Void is Not Assignable to Type Boolean (React/Typescript) React typescript - 类型“boolean”不可分配给类型 - React typescript - Type 'boolean' is not assignable to type 如何修复错误类型 void is not assignable to type using react and typescript? - How to fix error type void is not assignable to type using react and typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM