方法无法读取状态

Method can not read state

我正在使用 react-native 和解析服务器。我正在实施用户注册,但出现此错误:

undefined is not object (evaluating 'this.state.username')

不应该跨 class 读取状态吗?

import React, {Component} from 'react';
import {Container, Content, Form, Item, Input, Button, Text} from 'native-base';
var Parse = require('parse/react-native');

export default class Singup extends Component {
  constructor(props) {
    super(props);
    this.state = {
      username: '',
      password: ''
    };
  }

  Buttonpress() {
    Parse.initialize("APPLICATION_ID");
    Parse.serverURL = 'http://***.***.**.**:1337/parse'

    var user = new Parse.User();
    user.set("username", this.state.username);
    user.set("password", this.state.password);

    user.signUp(null, {
      success: function(user) {
        console.warn("YES");
      },
      error: function(user, error) {
        // Show the error message somewhere and let the user try again.
        alert("Error: " + error.code + " " + error.message);
      }
    });
  }

  render() {
    return (
      <Container>
        <Content>
          <Form>
            <Item>
              <Input
                placeholder="Username"
                onChangeText={(text) => this.setState({username: text})}
                value = {this.state.username}/>
            </Item>
            <Item last>
              <Input
                placeholder="Password"
                onChangeText={(text) => this.setState({password: text})}
                value = {this.state.password}/>
            </Item>
          </Form>
          <Button
            block
            onPress={this.Buttonpress}>
            <Text>Inscrever-se</Text>
          </Button>
        </Content>
      </Container>
    );
  }
}

自动翻译。

绑定你的函数应该对你有帮助

  constructor(props) {
    super(props);
    this.state = {
      username: '',
      password: ''
    };
    this.Buttonpress = this.Buttonpress.bind(this);
  }

引用如下:https://facebook.github.io/react/docs/react-without-es6.html#autobinding