简体   繁体   English

你如何键入一个作为道具传递给组件的 useState 钩子,typescript + react

[英]How do you type a useState hook that is being passed as a prop to a component, typescript+react

I have two components and我有两个组件和

Now I'm passing a useState hook from Admin component into the Login component as a setAuth prop现在我将一个 useState 挂钩从 Admin 组件传递到 Login 组件作为 setAuth 道具

In my Admin component:在我的管理组件中:

const Admin = () => {
  const [auth, setAuth] = useState(false);

  <Login setAuth={setAuth} />
}

And my Login component:我的登录组件:

const Login = ({ setAuth }) => {
  //and then logic to set auth based on db auth from firebase
  setAuth(true)
}

How do I properly type the setState hook being passed as a prop to a component?如何正确键入作为组件传递给组件的 setState 挂钩?

interface ISetAuth {
  setAuth: Dispatch<SetStateAction<boolean>>;
}

const Login = ({ setAuth }: ISetAuth) => {
  //whatever code
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM