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