【发布时间】:2020-11-01 08:20:29
【问题描述】:
我有基本组件,假设BaseContainer 连接到redux 并有一些方法。现在我想创建几个CustomContainer 组件,它们也应该连接到redux,并且应该可以访问BaseContainer 组件的所有方法和状态。
所以BaseContainer 将是:
class BaseContainer extends React.Component {
state = {};
method1() {};
method2() {};
method3() {};
}
export default connect(mapStateToProps, mapDispatchToProps)(BaseContainer);
CustomContainers 之一应该是:
class CustomContainer extends BaseContainer {
// should have access to all imports, methods and props of BaseContainer
}
export default connect(mapStateToProps, mapDispatchToProps)(CustomContainer);
试过这个,但似乎继承在 React 中不能很好地工作,也不推荐。
在这里我收到错误Super expression must either be null or a function。
尝试使用 HoC 的其他方法:
class CustomContainer extends React.Component {
// should have access to all imports, methods and props of BaseContainer
}
export default connect(mapStateToProps, mapDispatchToProps)(BaseContainer(CustomContainer));
现在我遇到了错误:Unhandled Rejection (TypeError): Object(...) is not a function
出了什么问题,我怎样才能让我的CustomContainer 可以访问BaseContainer 的所有导入、道具和状态?
【问题讨论】:
标签: reactjs inheritance redux higher-order-components