【问题标题】:Undefined is not an object (evaluating 'this.state.username') [duplicate]未定义不是对象(评估'this.state.username')[重复]
【发布时间】:2018-10-06 07:53:14
【问题描述】:

我是 React Native 的新手,刚开始学习它。 我想使用 React Native 构建 Android 应用。

现在,我有一个基本的登录表单,它将向服务器发送获取请求并显示用户是否被授权。

但是当我尝试提醒输入字段的值时,它会抛出错误“未定义不是对象”

我指的是官方文档here,此代码有效。 我也尝试过基本的反应形式 here 。 我使用UI kitten text input 进行输入,我也使用了本机反应TextInput,它给出了同样的错误。

我当前的代码是:

import React from 'react';
import { Alert, StyleSheet, Text, TextInput, View } from 'react-native';
import {RkButton, RkText, RkTextInput, RkCard} from 'react-native-ui-kitten';
import Icon from 'react-native-vector-icons/Ionicons';

export default class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      username: '',
      password: ''
    };

    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(event) {
    const target = event.target;

    this.setState({
      username: target.username,
      password: target.password
    });
  }


  login()
  {
    Alert.alert(this.state.username) //getting error here

    let data = {
      method: 'POST',
      credentials: 'same-origin',
      mode: 'same-origin',
      body: JSON.stringify({
        username: this.state.username,
        password: this.state.password
      }),
      headers: {
        'Accept':       'application/json',
        'Content-Type': 'application/json',
      }
    }
    fetch('http://demourl.com/login',data)
    .then((response) => response.json())
    .then((responseJson) => {
      if (responseJson.success == 'true') {
        Alert.alert('Login Successfull!')
      }
      else {
        Alert.alert('Login Failed')
      }
    })
    .catch((error) => {
      console.error(error);
    });
    ;
  }

  render() {
    return (
      <View style={styles.container}>
        <RkCard style={{padding: 20}}>
          <View style={{alignItems: 'center',padding: 10}}>
            <RkText rkType='warning large' style={{fontSize: 40}}>Log In</RkText>
          </View>
          <View style={{alignItems: 'center',padding: 10}}>
            <RkTextInput placeholder='Username' value={this.state.username} onChange={this.handleChange} />
            <RkTextInput placeholder='Password' value={this.state.password} onChange={this.handleChange}/>
          </View>
          <View style={{alignItems: 'center',padding: 10}}>
            <RkButton rkType='success small' title="Press Me"
              onPress={this.login}
            >Log In!</RkButton>
          </View>
        </RkCard>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    marginTop: 65,
    backgroundColor: '#fff',
    justifyContent: 'center',
    padding: 10
  },
});

请告诉我我在哪里无法理解工作。或者我的代码有问题。 提前致谢。

【问题讨论】:

  • onPress={this.login} 更改为 onPress={() =&gt; this.login()}

标签: javascript react-native react-native-android react-native-ui-kitten


【解决方案1】:

像绑定handleChange 一样绑定login 函数,或者像使用arrow 函数一样

login = () => {
  ...
}

看一下箭头函数文档here

希望这会有所帮助!

【讨论】:

  • 我在构造函数中将登录函数绑定为 this.login=this.login.bind(this);...错误现在消失了..谢谢....但我仍然不能在警报中访问用户名...它显示为空白...我在我的handlechange函数中警告target.username...它也在那里空白...我如何访问我在handlechange函数中的输入...我的代码是否错误这样做?
  • 对不起,我从来没有使用过这个库,但是根据他们的代码,你可以传递任何有效的 TextInput 属性,所以如果你只需要文本然后使用onChangeText。看看这个答案stackoverflow.com/questions/44416541/…
  • onChangeText 为我工作...谢谢!
猜你喜欢
  • 2018-10-24
  • 1970-01-01
  • 2016-12-04
  • 2019-09-26
  • 2019-12-12
  • 2019-08-16
  • 2017-01-16
  • 2020-08-06
  • 2020-01-09
相关资源
最近更新 更多