从react redux中的dispatch导航到另一个页面

问题描述 投票:0回答:1

我想在使用react redux成功登录后导航到配置文件页面但是它给出了错误:

[未处理的承诺拒绝:TypeError:undefined不是对象(评估'_this3.props.navigation')]

import React from 'react';
import { StyleSheet, Text, View, Button, TextInput, Image, TouchableHighlight } from 'react-native';
import { connect } from 'react-redux';
import axios from 'axios';
import { Actions } from 'react-native-router-flux';


class LogIn extends React.Component {

  
  state = {
    user_name: "",
    password: "",
  } 

  render(){
    return(
      <View style={styles.container}>       
                 
      <View  style={styles.inputContainer}>
      <TextInput style={styles.inputs}
              placeholder="User Name"
              underlineColorAndroid='transparent'
              onChangeText={(text)=>this.setState({user_name: text})}/>
      </View>          
      <View style={styles.inputContainer}>
      <TextInput style={styles.inputs}
              placeholder="Password"
              secureTextEntry={true}
              underlineColorAndroid='transparent'
              onChangeText={(text)=>this.setState({password: text})}/>
      </View>        
      <View style={styles.btn}>
      <TouchableHighlight style={[styles.buttonContainer, styles.loginButton]}  onPress = {()=>this.props.logIn(this.state)}>
         <Text style={styles.loginText}>Login</Text>
      </TouchableHighlight> 

       <TouchableHighlight style={styles.buttonContainer}>
            <Text>Forgot your password?</Text>
        </TouchableHighlight>
                
      <TouchableHighlight style={[styles.buttonContainer, styles.loginButton]} onPress = {()=>navigation.navigate('SignUp')}>  
      <Text>Register here</Text>
        </TouchableHighlight>
      
      </View>             
      </View>      
      );
   }
}

function mapStateToProps (state){
  return {    
    user_name: state.user_name,
    password: state.password,
  }
}


function mapDispatchToProps(dispatch){

  return{
    logIn: (text) => {
      if(text.user_name == ""){
        alert("Enter user_name");       
      }
      else if(text.password == ""){
        alert("Enter Password");       
      }      
      else {
        var body = {email: text.user_name,password: text.password}
        console.log("body",body);
        axios.post('http://users/user/login',body)
        .then(res=>{
             dispatch({ type: 'LOG_IN',payload: res});
             this.props.navigation.navigate('AppDrawer');
        },err=>{         
             alert(err);         
        }) 
      }      
    }
  }
}


export default connect(mapStateToProps,mapDispatchToProps)(LogIn)

我也使用了NavigationActions但出现了同样的错误。 我也通过导航道具,但它给出了同样的错误。

javascript react-native react-redux react-navigation
1个回答
1
投票

mapDispatchToProps函数在您的反应组件之外定义... this上下文不引用React组件,因此您可以访问this.props.navigation

通过mapDispatchToProps的第二个可选参数访问您的组件道具:

const mapDispatchToProps = (dispatch, ownProps) => {
  // access ownProps.navigation 
};
© www.soinside.com 2019 - 2024. All rights reserved.