reactjs - 不同类型的函数参数
问题描述
我有一个 onChange 函数,我将它作为道具传递给另一个 React 组件。
我将组件的 Props 声明如下:
onChange: (newValue: ContentManagementCategoryType[] | OrganizationType[]) => void;
我试图在组件中传递的 onChange 具有以下格式:
const updateFilterCategories = (newValue: ContentManagementCategoryType[]) => {
setFilteredCategories(newValue);
};
所以我收到以下错误:
类型 '(newValue: ContentManagementCategoryType[]) => void' 不可分配给类型 '(newValue: ContentManagementCategoryType[] | OrganizationType[]) => void'。参数“newValue”和“newValue”的类型不兼容。
我无法弄清楚为什么会出现错误,因为该组件正在等待一个带有newValue作为ContentManagementCategoryType[]或OrganizationType[]的 onChange 并且我正在传递一个带有 ContentManagementType[]的 onChange 。
有什么解决办法吗?
先感谢您。
解决方案
尝试这个
type ValueType = (ContentManagementCategoryType | OrganizationType)[]
onChange: (newValue: ValueType) => void;
const updateFilterCategories = (newValue: ValueType) => {
setFilteredCategories(newValue);
};
推荐阅读
- hadoop - 如何解决我在 Windows 10 中的 Eclipse 中执行 map-reduce java 代码时遇到的这个问题?
- javascript - 为新对象创建嵌套对象
- generics - 如何迭代实现 Index 和 IntoIterator 的通用集合的索引?
- javascript - 帖子 ID:未定义
- javascript - 悬停在无形区域上的动画
- xml - 元素类型“xsd:sequence”必须以匹配的结束标签“”结束
- python - Python - 为具有 Null/NA 的特定列连接错误消息
- reactjs - 如何避免空数组的流输入错误?
- android - 应用程序未显示在 Google Playstore 中按关键字搜索的结果列表中
- java - 由于 nextInt() 导致的 java.util.NoSuchElementException 类型的异常