![](/img/trans.png)
[英]How do I define a TypeScript type for React props where prop B is only accepted if prop A is passed to a component?
[英]How do you type a useState hook that is being passed as a prop to a component, typescript+react
我有兩個組件和
現在我將一個 useState 掛鈎從 Admin 組件傳遞到 Login 組件作為 setAuth 道具
在我的管理組件中:
const Admin = () => {
const [auth, setAuth] = useState(false);
<Login setAuth={setAuth} />
}
我的登錄組件:
const Login = ({ setAuth }) => {
//and then logic to set auth based on db auth from firebase
setAuth(true)
}
如何正確鍵入作為組件傳遞給組件的 setState 掛鈎?
interface ISetAuth {
setAuth: Dispatch<SetStateAction<boolean>>;
}
const Login = ({ setAuth }: ISetAuth) => {
//whatever code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.