2017-09-03 3 views
0

私は反応ネイティブの反応ナビゲーションを使用しています。私がrender()作品の中でthis.props.navigation.dispatch (resetAction)を呼び出すと。すでにButtonpress()の中にあります。私の目標は、Parse Serverがユーザーを認証してルートをリセットするときに別のルートに移動することです。メソッド内でreact bind resetActionを使用するにはどうすればよいですか?

自動的に翻訳されます。あなたはそれが別のコンテキスト内だから、それはどこか、どこthis != thisから呼び出されますので、現在のコンテキストにあなたの機能をbindする必要が

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

import {NavigationActions} from 'react-navigation' 

const resetAction = NavigationActions.reset({ 
    index: 0, 
    actions: [NavigationActions.navigate({routeName: 'Main'})] 
}) 

export default class Auth extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     username: '', 
     password: '' 
    }; 
    this.Buttonpress = this.Buttonpress.bind(this); 
    } 
    static navigationOptions = { 
    title: 'Hedone' 
    }; 

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

    Parse.User.logIn(this.state.username, this.state.password, { 
     success: function(user) { 
     // Do stuff after successful login. 
     () => this.props.navigation.dispatch(resetAction) 
     console.warn("Certo") 
     }, 
     error: function(user, error) { 
     // The login failed. Check error to see why. 
     console.warn("Errado"); 
     } 
    }); 
    } 

    render() { 
    const {navigate} = this.props.navigation; 
    return (
     <Container> 
     <Content> 
      <Form> 
      <Item> 
       <Input placeholder="Username" onChangeText={(text) => this.setState({username: text})}/> 
      </Item> 
      <Item last> 
       <Input placeholder="Password" onChangeText={(text) => this.setState({password: text})}/> 
      </Item> 
      </Form> 

      <Button block onPress={this.Buttonpress}> 
      <Text>Entrar</Text> 
      </Button> 

      <Button block onPress={() => navigate('Up')}> 
      <Text>Inscrever-se</Text> 
      </Button> 
     </Content> 
     </Container> 
    ); 
    } 
} 

答えて

1

。現在のthisコンテキストからいくつかの変数/関数を使いたい場合にのみ行う必要があります。

Parse.User.logIn(this.state.username, this.state.password, { 
    // needs to be bind because you want use 
    // `this.props` from the current context 
    success: function (user) { 
     // Do stuff after successful login. 
     this.props.navigation.dispatch(resetAction) 
     console.warn("Certo") 
    }.bind(this), 
    // this doesn't need to be bind, because it 
    // doesn't use something from this context 
    error: function (user, error) { 
     // The login failed. Check error to see why. 
     console.warn("Errado"); 
    } 
}); 
+0

ありがとうございました! –

関連する問題