![](/img/trans.png)
[英]How to fix error “argument of type false is not assignable to (currentValue: boolean) => boolean” using react and typescript?
[英]How to fix error "argument of type (open: any) => boolean is not assignable to parameter of type boolean" using react and typescript?
我想使用 react 和 typescript 修復錯誤“類型參數(打開:任何)=> 布爾值不可分配給布爾類型的參數”
我有一個方法 toggleDialogVisibility 定義如下
export const useDialog() {
const {setDialogVisibility} = React.useContext(DialogContext);
return (
const toggleDialogVisibility = (toggleValue: boolean) => {
setDialogVisible(toggleValue);
};
);
}
我在下面的其他組件中調用它,
function Parent() {
const {toggleDialogVisibility} = useDialog();
return (
<div onClick={() => toggleDialogVisibility(open => !open)} /> //getting error here
);
}
我不知道如何在這里傳遞什么類型。 有人可以幫我解決這個問題嗎? 謝謝。
參數函數toggleDialogVisibility
的類型是布爾值。 您應該發送一個布爾值而不是一個回調函數。
() => toggleDialogVisibility(!open)}
在父組件中發送切換值,而不僅僅是為我解決它的函數:
const [toggle, setToggle] = useState<boolean>(false);
<EditForm setToggle={setToggle} open={toggle}/>
在子組件中:
interface Props{
open: boolean,
setToggle: (toggle: boolean) => void
}
setToggle(!open);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.