reactjs - 具有高阶组件的通用组件
问题描述
我想给我的 React 组件 props 一个泛型类型,但是当我将它包装在更高阶的组件(material-ui)中时,它会丢失我如何传递所需的信息?
type Props<T> = {
data: Array<T>;
}
class MyComponent<T> extends React.Component<Props<T>> {
const StyledComponent = withStyles(styles)(MyComponent)
Using<StyledComponent<myType
给出一个错误,因为它不知道泛型。
解决方案
我敢打赌,像这样注释您的新组件:
const StyledComponent: <T>(props: OuterProps<T>) => JSX.Element = withStyles(styles)(MyComponent) ;
请注意,您可能需要区分OuterProps
和InnerProps
,请参阅以下示例以供参考:
https://stackblitz.com/edit/hoc-generics
希望有帮助!
推荐阅读
- angular - 离子角电容器 - 未找到 FileOpener 活动:未找到处理意图的活动
- android - 检查 VPN 上的 Internet 连接
- javascript - [Vue 警告]:指令中的错误 intersect unbind 挂钩:“TypeError:无法读取未定义的属性‘观察者’”
- docker - 如何调整 Jenkinsfile 中的 ContainerTemplate 以从私有注册表获取图像
- python - 输入并保存用户名和密码到同一个文件
- php - PHP & XML:带冒号的 XML 响应的访问属性?
- node.js - 使用 Angular 13 安装 ngrx 商店时出现错误无法解析依赖关系树
- react-native - React Native Map 无法动态渲染多个不同的图像数组标记
- spring-boot - 测试未找到 Springboot Webflux 端点 - 是什么原因造成的?
- pandas - 以特定方式重塑熊猫数据框