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;

也许你可以试试这个:

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

让我们尝试使用()

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

Rizal 是对的,在你的第二个示例中,你controlMethod函数传递给了onPressprop,当你点击<Button>组件时,controlMethod将被执行,但在第二个示例中,controlMethod实际上不会执行Actions.login2函数,而是会返回函数表达式,所以要使其工作, 你想实际调用Actions.login2这就是为什么你需要在Actions.login2之后再加一对括号,就像 Rikal 的示例所示。

此外,请确保在构造函数中绑定controlMethod,或者可以改为将其设置为静态方法。

最新更新