无法读取属性'道具'未定义的

时间:2016-07-15 03:19:14

标签: javascript reactjs react-native

你必须对我很轻松......对React Native来说是个新手

我有这段代码,非常简单:

export default class signup extends Component {

      constructor(props){
        super(props);

        this.state = {
          loaded: true,
          email: '',
          password: ''
        };
      }

      signup(){

        this.setState({
          loaded: false
        });




    firebaseApp.auth().signInWithEmailAndPassword(this.state.email, this.state.password).then(function() {

      this.props.navigator.push({
          component: Login
        });

    }, function(error) {
      // An error happened.
      var errorCode = error.code;
      var errorMessage = error.message;
      console.log("ERROR WITH SIGNIN")
      console.log( errorMessage )
    });




      }

      goToLogin(){
        this.props.navigator.push({
          component: Login
        });
      }

      render() {
        return (
          <View style={styles.container}>
            <Header text="Signup" loaded={this.state.loaded} />
            <View style={styles.body}>

              <TextInput
                style={styles.textinput}
                onChangeText={(text) => this.setState({email: text})}
                value={this.state.email}
                placeholder={"Email Address"}
              />
              <TextInput
                style={styles.textinput}
                onChangeText={(text) => this.setState({password: text})}
                value={this.state.password}
                secureTextEntry={true}
                placeholder={"Password"}
              />
              <Button 
                text="Signup" 
                onpress={this.signup.bind(this)} 
                button_styles={styles.primary_button} 
                button_text_styles={styles.primary_button_text} />

              <Button 
                text="Got an Account?" 
                onpress={this.goToLogin.bind(this)} 
                button_styles={styles.transparent_button} 
                button_text_styles={styles.transparent_button_text} />
            </View>
          </View>
        );
      }
    }

    AppRegistry.registerComponent('signup', () => signup);

我不明白的是,如果我点击&#34;转到登录&#34;按钮它完美地运行功能并转到登录页面

 goToLogin(){
    this.props.navigator.push({
      component: Login
    });
  }

但是当我点击注册按钮时,它会抛出错误

  

无法阅读属性&#39;道具&#39;未定义的

我想我可以为它运行相同的代码块:

 this.props.navigator.push({
          component: Login
        });

但是它会导致错误。我有点困惑,为什么会发生这种情况,因为它在其他功能中完美运行..我确信这只是我和我对反应原生的缺乏理解。 谢谢你们!

1 个答案:

答案 0 :(得分:2)

您需要使用绑定或箭头功能

firebaseApp.auth().signInWithEmailAndPassword(this.state.email, this.state.password).then(function() {

  this.props.navigator.push({
      component: Login
    });

}.bind(this), function(error) {
  // An error happened.
  var errorCode = error.code;
  var errorMessage = error.message;
  console.log("ERROR WITH SIGNIN")
  console.log( errorMessage )
});
  

OR

firebaseApp.auth().signInWithEmailAndPassword(this.state.email, this.state.password).then(() => {

  this.props.navigator.push({
      component: Login
    });

}, function(error) {
  // An error happened.
  var errorCode = error.code;
  var errorMessage = error.message;
  console.log("ERROR WITH SIGNIN")
  console.log( errorMessage )
});