简体   繁体   English

React Native无法读取属性未定义错误

[英]React Native cannot read property undefined error

I'm starting out on react native and I'm trying to do a simple console log the value from the text box when a button is pressed. 我正在开始对本机进行响应,并且尝试通过按下按钮从文本框中输入一个简单的控制台日志值。

I am getting the error 我收到错误

Cannot read property 'email' of undefined 无法读取未定义的属性“电子邮件”

. The error is happening on the console.log. 该错误发生在console.log上。 What am I doing wrong here? 我在这里做错了什么?

LoginPage.js LoginPage.js

import React, {Component} from 'react';
import {AppRegistry, Text, View, TextInput, Button} from 'react-native';


export default class LoginPage extends Component{
  constructor(){
    super()

    this.state = {
      email: '',
      password: '',
    }
  }

  static navigationOptions = {
    headerTitle: 'Login',
  };

  getUser(){
  /*  fetch('http://192.168.1.12:8000/login')
      .then(response => response.json())
      .then(response => {
        console.log(response);
      });
  */
    console.log(this.state.email);
    console.log(this.state.password);
  }

  render(){
    return(
      <View>
        <Text>Login Page</Text>


        <TextInput
          placeholder = "Email"
          onChangeText={(text) => this.setState({email: text})}
          //value={this.state.textValue}
          //onChangeText={(value) => this.onChangeText(value)}
          //onSubmitEditing={this.onSubmit}
        />

        <TextInput
          placeholder = "Password"
          secureTextEntry={true}
          onChangeText={(text) => this.setState({password: text})}
          //value={this.state.textValue}
          //onChangeText={(value) => this.onChangeText(value)}
          //onSubmitEditing={this.onSubmit}
        />

        <Button
          //onPress={() => this.props.navigation.navigate('RegisterPage')}
          title="Login"
          accessibilityLabel="Login"
          onPress={this.getUser}
        />

      </View>
    );
  }
}

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

You need to bind the getUser function to the class instance. 您需要将getUser函数绑定到类实例。 You can do that in two way. 您可以通过两种方式做到这一点。 One is doing it in constructor like this 一个正在这样的构造函数中做

constructor(){
    super()

    this.state = {
      email: '',
      password: '',
    }
    this.getUser = this.getUser.bind(this);
  }

Or you can use arrow function syntax like this 或者您可以使用像这样的arrow function语法

getUser = () => {
    console.log(this.state.email);
    console.log(this.state.password);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法读取未定义的React Native属性'push' - Error: Cannot read property 'push' of undefined React Native 反应本机导航错误-TypeError:无法读取未定义的属性'catch' - React native navigation error -TypeError: Cannot read property 'catch' of undefined 无法读取属性 '#<webview> ' 未定义的反应本机</webview> - Cannot read property '#<WebView>' of undefined react native 反应本机“无法读取未定义的属性” AsyncStorage - React native 'Cannot read property of undefined' AsyncStorage React Native 无法读取未定义的属性“导航” - React Native Cannot read property 'navigate' of undefined React Native:错误:“无法读取未定义的属性‘名称’,”虽然包含名称的对象不是未定义的 - React Native: Error:"Cannot read property 'name' of undefined, " Although object which contains name is not undefined 反应本机如何处理无法读取未定义的属性“未定义” - react native how to deal with Cannot read property 'undefined' of undefined React组件:无法读取未定义错误的属性 - React component: cannot read property of undefined error 反应错误-无法读取未定义的属性“状态” - React Error - Cannot read property 'state' of undefined 反应错误 - 无法读取未定义的属性“setState” - React error - Cannot read property 'setState' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM