将挂钩传递给孩子打字稿的问题 [英] issue with passing hook to child typescript

查看:108
本文介绍了将挂钩传递给孩子打字稿的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用钩子的react组件.我的父组件看起来像这样:

I have a react component using hooks. My parent component looks like this:

const Parent = () => {

   const [isActive, setIsActive] = useState(false);

   return (
     <Child isActive={isActive} setIsActive={setIsActive} />
   );
}

这是子组件

type Props = {
   isActive: boolean;
   setIsActive: () => void;
}
const Child = ({ isActive, setIsActive}: Props) {
   // component
} 

我看到的错误是

类型错误:类型为'Dispatch< SetStateAction>'不可分配给>类型'()=> void'. TS2322

Type error: Type 'Dispatch < SetStateAction>' is not assignable to > type '() => void'. TS2322

推荐答案

ChildProps类型不正确. React将setIsActive键入为Dispatch,其定义为:

Your Props type for Child is incorrect. React types the setIsActive as Dispatch, which is defined as:

type Dispatch<A> = (value: A) => void;

您缺少类型中的value参数.这应该是正确的(还请注意,它必须是冒号而不是等号):

You're missing the value argument from your type. This should be correct (also note that it needs to be a colon not an equal sign):

type Props = {
   isActive: boolean;
   setIsActive: (active: boolean) => void;
}
const Child = ({ isActive, setIsActive}: Props) {
   // component
} 

这篇关于将挂钩传递给孩子打字稿的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆