简体   繁体   English

React Native组件不能使用超级

[英]React Native Component cannot use super

I've created a component with a few functions for React components that I'd like to use in multiple components. 我已经创建了一个组件,其中包含一些我想在多个组件中使用的React组件的功能。

I've tried implementing it, but whenever I try to call super.updateHeader , it crashes with error 我已经尝试实现它,但是每当我尝试调用super.updateHeader ,它就会因错误而崩溃

ExceptionsManager.js:84 Unhandled JS Exception: TypeError: TypeError:  
Cannot read property 'call' of undefined

Using super in the constructor works without an issue. 在构造函数中使用super可以正常工作。 Trying to reach the function with this.updateHeader results in 尝试使用this.updateHeader达到功能this.updateHeader导致

Unhandled JS Exception: TypeError: TypeError: this.updateHeader is not a function

Neither updateHeader nor the function I'm calling it from are arrow functions. updateHeader或我从中调用的函数都不是箭头函数。

How do I solve this? 我该如何解决? (They're instance based, so I'd like to stay away from creating some sort of library for this) (它们是基于实例的,因此我想避免为此创建某种库)

SuperComponent: SuperComponent:

class CustomRouteViewComponent extends Component {
    updateHeader(props) {
        const {settings, navigation} = props;

        props.navigation.setParams({
            headerTintColor: settings.theme === 'dark' ? commonColorDark.textColor : commonColor.textColor,
            backgroundColor: settings.theme === 'dark' ? commonColorDark.headerStyle : commonColor.headerStyle,
        });
    };
}
const mapStateToProps = state => ({settings: state.settings});

export default connect(mapStateToProps)(CustomRouteViewComponent);

HomeScreen 主屏幕

class Home extends CustomRouteViewComponent {
    componentWillMount() {
        this.updateHeader(this.props);
    }
    render() {
        return (
            <View><Text>Hello!</Text></View>
        )
    }
}
const mapStateToProps = state => ({});

export default connect(mapStateToProps)(Home);

The problem is that OOP inheritance is mixed with functional approach (Redux connect ). 问题是OOP继承与功能方法(Redux connect )混合在一起。 CustomRouteViewComponent in Home module is connected functional component and not original class component. Home模块中的CustomRouteViewComponent是连接的功能组件,而不是原始的类组件。

CustomRouteViewComponent class could be exported for inheritance purposes, or original class could be reached on connected component as: 可以出于继承目的导出CustomRouteViewComponent类,或者可以在连接的组件上通过以下方式访问原始类:

class Home extends CustomRouteViewComponent.WrappedComponent {...}

This will result in problems because CustomRouteViewComponent already relies on connected props, but its own mapStateToProps won't be taken into account, ie there won't be props.settings in Home . 这将导致问题,因为CustomRouteViewComponent已经依赖于连接的道具,但是不会考虑其自身的mapStateToProps ,即Home中将没有props.settings

A proper solution is to not mix OOP with functional programming in this case. 在这种情况下,正确的解决方案是不要将OOP与功能编程结合使用。 The behaviour from CustomRouteViewComponent could be achieved via HOC or the composition of mapStateToProps functions. CustomRouteViewComponent的行为可以通过HOC或mapStateToProps函数的组合来实现。

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

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