2017-07-21 15 views
1

私は反応ネイティブと解析サーバーを使用しています。私は、ユーザー登録を実施していますが、このエラーが表示されます。メソッドが状態を読み取ることができません

未定義のは(「this.state.username」を評価)

オブジェクトされていない状態がクラス全体に読まれるべきではありませんか?

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> 
    ); 
    } 
} 

自動的に翻訳されます。

あなたの関数の結合
+0

[SETSTATE再利用可能に反応](https://stackoverflow.com/questions/41887440/react-の可能性のある重複setstate-reusable) –

+0

ButtonPress関数とのバインディングの問題は、重複を参照してください –

+0

この質問はその作成者によって放棄されました。正解はありませんでした。 –

答えて

2

はここであなたに

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

+0

この質問には複数の重複がありますStackOverflow、重複としてマークするか、それに答えるのではなく適切なリンクを提供することを検討してください。 –

+0

ありがとうございました!なぜ私がこれを必要としているかの参考にしてもらえますか?私は理解したいです。 –

+0

[This one](https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56)は、変異に関するバインディングに関するものです。 [Mozilla docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) – Andrew