react-native-router-flux来自函数的调用操作

时间:2017-07-18 08:23:47

标签: reactjs react-native react-native-router-flux

这是我目前正在使用的登录页面。一切正常

import React, { Component } from 'react';
import {AlertIOS , StyleSheet , View , Text} from 'react-native';
import Button from 'react-native-button';
import {Actions } from 'react-native-router-flux';

class Login extends Component {
    render() {
        return (
           <View>
               <Text>Hello world this is login page</Text>
               <Button onPress={Actions.login2}>Click </Button>
           </View>
        );
    }
}

export default Login;

但是,我想首先调用函数来检查用户名和密码。如果它是正确的,我想转到下一个屏幕。我怎样才能做到这一点。喜欢下面这段代码。我真正的问题是我无法弄清楚在渲染方法之外调用Actions.ROUTE_NAME方法。

//This is not working
import React, { Component } from 'react';
import {AlertIOS , StyleSheet , View , Text} from 'react-native';
import Button from 'react-native-button';
import {Actions } from 'react-native-router-flux';

class Login extends Component {
controlMethod(){
if(true){Actions.login2}
}
    render() {
        return (
           <View>
               <Text>Hello world this is login page</Text>
               <Button onPress={this.controlMethod}>Click </Button>
           </View>
        );
    }
}

export default Login;

2 个答案:

答案 0 :(得分:1)

也许你可以尝试这个:

controlMethod(){
  if(true){ Actions.login2() }
}

让我们尝试使用()

希望它能解决你的问题:)

答案 1 :(得分:0)

Rizal是对的,在您的第二个示例中,您将controlMethod函数传递给onPress道具,当您点击<Button>组件时,controlMethod将被执行,但是在你的第二个例子中,controlMethod实际上不会执行Actions.login2函数,而是会返回function expression,所以为了使它工作,你想要实际调用{{1}这就是为什么你需要在Actions.login2之后的另一对括号,就像Rizal的例子所示。

另外,请确保在构造函数中绑定Actions.login2,或者将其设为静态方法。

相关问题