Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在某些组件中使用HOC,而不是Redux中的每个组件_Reactjs_React Native_Redux_React Redux_Higher Order Components - Fatal编程技术网

Reactjs 在某些组件中使用HOC,而不是Redux中的每个组件

Reactjs 在某些组件中使用HOC,而不是Redux中的每个组件,reactjs,react-native,redux,react-redux,higher-order-components,Reactjs,React Native,Redux,React Redux,Higher Order Components,首先,我将componentA、componentB、componentC连接到redux。其次,从BackHandlerHoc导出组件A import React from 'react' import { BackHandler,Platform} from "react-native"; export default function BackHandlerHoc(WrappedComponent) { return class extends React.Component

首先,我将componentA、componentB、componentC连接到redux。其次,从BackHandlerHoc导出组件A

import React from 'react'
import { BackHandler,Platform} from "react-native";


export default function BackHandlerHoc(WrappedComponent) {
    return class extends React.Component {
        constructor(props) {
            super(props)
        }

        componentDidMount() {
            if (Platform.OS === "android") {
                BackHandler.addEventListener('hardwareBackPress', this.goBack.bind(this));
            }
        }

        componentWillUnmount() {
            if (Platform.OS === "android") {
                BackHandler.removeEventListener("hardwareBackPress", this.goBack.bind(this));
            }
        }


        goBack = () => {
            console.log("goBack")
            // some code
        };

        render() {
            return (
                <WrappedComponent {...this.props} />
            )
        }

    }
}
在组件B中

export default connect(mapStateToProps, null)(componentB)
在组件C中

export default connect(mapStateToProps, null)(componentC)
问题是每个连接到redux(componentB、componentC、componentA)的组件都会得到BackHandler.addEventListener。 我只需要componentA获取BackHandler.addEventListener。
我该怎么办

我真的不明白您的问题是什么,您希望componentA的某些实例不具有事件侦听器行为?componentA接受addEventListener,但不只是componentA。我的项目中的每一个元素都会增加一个列表。这就是使用redux的原因。
export default connect(mapStateToProps, null)(componentC)