我正在向我的应用程序添加一个简单的登录屏幕,但一旦用户通过身份验证,我将无法调用 Actions.home。
我有一个按钮,按下该按钮时,会调用一个函数来连接到服务器并获取身份验证状态,成功后,我会调用 Actions.home。但什么也没发生。没有错误或警告。根本不值一提。
我已经尝试了所有形式,Actions.home、Actions.home()、{Actions.home},将 Actions.home 保存为构造函数中的状态,然后使用状态,...没有任何效果。
但在我的其他屏幕中,当我在 onPress 道具中调用 Actions.somewhere 时,它会起作用。
我阅读了这里的大部分问题,以及 StackOverflow 上的问题(数量不多),但无法理解出了什么问题。我使用了到处建议的所有解决方案,但什么也没用。
当我 console.log(Actions.home) 时,我看到的是:在此输入图像描述 https://i.stack.imgur.com/oIaN6.png
这是我的脚本:
路由器.js
<Scene key="root">
<Scene key="login" title="login" component={Login} hideTabBar hideNavBar initial />
<Scene key="tabsRoot" tabs tabBarStyle={[styles.tabBar, styles.tabBarShadow]} >
<Scene key="tab_home" iconName="home" icon={TabBarItem} >
<Scene key="home" title="saba" component={Home} navBar={NavBar} />
<Scene key="campaignHome" title="campaign" hideTabBar component={Campaign} navBar={NavBar} direction="vertical" />
</Scene>
......
</Scene>
</Scene>
登录屏幕:
export
default class Login extends Component {
constructor(props) {
super(props);
this.state = {
name: '',
phone: '',
shouldLogin: false,
shouldRegister: false,
error: false,
};
this._checkAuth = this._checkAuth.bind(this);
this._onNumberChanged = this._onNumberChanged.bind(this);
this._isRegistered = this._isRegistered.bind(this);
this._isNotRegistered = this._isNotRegistered.bind(this);
}
async _isRegistered(response) {
var user = response.payload.user;
this.state = {
name: user.name,
phone: user.phone,
shouldLogin: true,
};
Actions.home;
}
_isNotRegistered(response) {
var error = response.error;
if (!error.code === NOT_REGISTERED_CODE) {
this.setState({
shouldLogin: false,
shouldRegister: false,
error: true,
})
throw new AuthException("server responded with unrecognised object");
}
this.setState({
shouldLogin: false,
shouldRegister: true,
error: false,
})
}
_checkAuth() {
var {
phone
} = this.state;
var data = {
phone: phone,
}
let url = buildQuery(AuthTypes.LOGIN, data);
console.log("usl: " + url);
//connect to server
....
if(response.success) {
this._isRegistered(response);
} else {
this._isNotRegistered(response);
}
}
_onNumberChanged(event) {
var phone = event.nativeEvent.text;
this.setState({
phone: phone,
});
}
render() {
return ( < View style = {
[styles.container]
} >
< View style = {
[styles.imgContainer]
} >
< Image source = {
require('./../Images/login.png')
}
width = "200"
height = "200" / >
< /View>
<View style={[styles.form]}>
<View style={[styles.inputContainer]}>
<TextInput style={[styles.input]} placeholder="enter your phone number" onChange={ this._onNumberChanged } maxLength={12} / >
< /View>
<TouchableWithoutFeedback onPress={ this._checkAuth } >
<View style={[styles.submitContainer]}>
<View>
<Text style={[styles.text, styles.loginButton]}>login</Text >
< /View>
</View >
< /TouchableWithoutFeedback>
</View >
< /View>
)
}
}
我认为问题在于您试图从登录转到嵌套在另一个场景内的场景。从登录的上下文中,您将拥有Actions.tabsRoot
因为它是相邻场景,所以可供您使用。
这是我使用构建的一个简单示例react-native-router-flux
来测试这个。
<Router>
<Scene key='pageOne' component={PageOne} initial={true}/>
<Scene key='home'>
<Scene key='pageTwo' component={PageTwo}/>
<Scene key='pageThree' component={PageThree}/>
</Scene>
</Router>
From pageOne我可以打电话Actions.home
并且它会转换到该堆栈中的第一个场景或该堆栈中具有以下内容的场景initial
打开。但是,如果我尝试打电话Actions.pageTwo
from pageOne这是行不通的。
希望这可以帮助。 :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)