Reactjs Undefined不是React Native中的对象(计算';this.props.dispatch';)

Reactjs Undefined不是React Native中的对象(计算';this.props.dispatch';),reactjs,react-native,redux,Reactjs,React Native,Redux,我正在使用React Native和Redux制作一个简单的计数器应用程序。我想要的是,当我点击加号按钮时,计数器将+1 但当我点击加号按钮时,错误 Undefined不是对象(正在计算'this.props.dispatch') 发生 import React, {Component} from 'react'; import {connect} from 'react-redux'; import {Text, View, StyleSheet, TouchableOpacity} from

我正在使用React Native和Redux制作一个简单的计数器应用程序。我想要的是,当我点击加号按钮时,计数器将+1

但当我点击加号按钮时,错误

Undefined不是对象(正在计算'this.props.dispatch')

发生

import React, {Component} from 'react';
import {connect} from 'react-redux';
import {Text, View, StyleSheet, TouchableOpacity} from 'react-native';
import {incrementCounter, decrementCounter} from '../actions';

class Main extends Component{

    incrementCounter(){
        this.props.dispatch(incrementCounter);
    };

    decrementCounter(){
        this.props.dispatch(decrementCounter);
    };
    render(){
        return(
            <View>
                <Text>RN + Redux Simple Counter</Text>
                <Text>{this.props.count}</Text>
                <View>
                    <TouchableOpacity onPress={this.incrementCounter}>
                        <Text>+</Text>
                    </TouchableOpacity>
                    <TouchableOpacity onPress={this.decrementCounter}>
                        <Text>-</Text>
                    </TouchableOpacity>
                    </View>
            </View>
        );
    }
}

const mapStateToProps = (state) => {
    return {
        count: state.count
    }
};

export default connect(
    mapStateToProps
)(Main)
import React,{Component}来自'React';
从'react redux'导入{connect};
从“react native”导入{文本、视图、样式表、TouchableOpacity};
从“../actions”导入{incrementCounter,decrementCounter};
类主扩展组件{
递增计数器(){
此.props.dispatch(递增计数器);
};
递减计数器(){
此.props.dispatch(递减计数器);
};
render(){
返回(
RN+Redux简单计数器
{this.props.count}
+
-
);
}
}
常量mapStateToProps=(状态)=>{
返回{
计数:state.count
}
};
导出默认连接(
mapStateToProps
)(主要)

怎么解决呢。提前感谢…

您需要将组件完全连接到redux。看

通过
mapDispatchToProps

const mapDispatchToProps = (dispatch) => {
  return {
    onIncrementClick: () => {
      dispatch(incrementCounter)
    }
  }
}
然后调用
this.props.onIncrementClick()

const mapDispatchToProps = (dispatch) => {
  return {
    onIncrementClick: () => {
      dispatch(incrementCounter)
    }
  }
}