未定义不是对象(评估“ this.props.dispatch”)

时间:2019-01-07 17:07:40

标签: react-native redirect

我有这个包含两个类的React本机组件,第一个有一个按钮,我希望它将我重定向到另一个页面,它不重定向任何东西,而只是派发错误。当我尝试使用第二类的重定向功能时,它可以工作。请问哪里出问题了?

   import React, { Component } from 'react';
      import {
       TouchableOpacity ,
    View,
    BackHandler,
     Image,
   Text,
    Button,

  Platform,
    ActivityIndicator
     } from 'react-native'; 

      import {connect } from 'react-redux'

  class HeaderBarWebView extends Component{
        constructor(props){

       super(props);
      }

      goCamera(){

        this.props.dispatch(changeCurrentPage("camera"))
        }

     render(){
     return(

      <View>

    <TouchableOpacity style={{justifyContent:'center',display:  'flex'}} 
       onPress={this.goCamera}>
      <Image
    style={{tintColor:"white" , height:30,
      width:30,marginRight:20}}
    source={require('../resource/img/camera.png')}
     />
    </TouchableOpacity>   

     </View>
             )}}

     class WebviewApp extends Component{

         constructor(props)
         {

       this.state={}
       this.goCamera= this.goCamera.bind(this)
         goCamera(){

      this.props.dispatch(changeCurrentPage("camera"))
                   }
     render()
      {
        return(
        <View style={{flex:1,width:"100%"}}>
                <Button title="Press me" onPress={this.goCamera}></Button>

        <CustomWebView
           ...........................
          />

          </View>
           );
         }
            }

     const mapStateToProps = state => ({
      page:state.router.page,

       camera : state.router.camera

                    })
     const WebviewAp = connect(mapStateToProps)(WebviewApp,HeaderBarWebView)
         export default WebviewAp

这是显示的错误

pic

1 个答案:

答案 0 :(得分:2)

您需要在构造函数中将goCamera函数与“ this”绑定;

constructor(props){
   super(props);
   this.goCamera = this.goCamera.bind(this);
}