React native 用户未经身份验证后,不会立即调用导航
我正在尝试注销用户,然后将其发送到SignedOut屏幕,但当我按下Sign Out时,它正在调用React native 用户未经身份验证后,不会立即调用导航,react-native,react-navigation,React Native,React Navigation,我正在尝试注销用户,然后将其发送到SignedOut屏幕,但当我按下Sign Out时,它正在调用unauthUser功能,但随后它将重新启动开关导航器到仪表板,我必须进入配置文件屏幕,再次点击Sign Out以退出。知道我做错了什么吗 这是Profile.js中的我的按钮 <TouchableOpacity onPress={() => onSignOut().then(() => { this.props.dispatch(unauthUser) n
unauthUser
功能,但随后它将重新启动开关导航器到仪表板,我必须进入配置文件屏幕,再次点击Sign Out以退出。知道我做错了什么吗
这是Profile.js中的我的按钮
<TouchableOpacity
onPress={() => onSignOut().then(() => {
this.props.dispatch(unauthUser)
navigation.navigate("SignedOut")
}
)}
>
<View style={styles.signOutButton}>
<Text style={styles.button}>SIGN OUT</Text>
</View>
</TouchableOpacity>
authoctions.js中的unauthUser
exports.unauthUser = {
type: 'UNAUTH_USER'
}
从authReducer.js
case 'UNAUTH_USER':
return {
user_id: undefined,
token: undefined
};
这是我的开关导航器
export const createRootNavigator = (signedIn = false) => {
return SwitchNavigator(
{
SignedIn: {
screen: SignedIn
},
SignedOut: {
screen: SignedOut
}
},
{
initialRouteName: signedIn ? "SignedIn" : "SignedOut"
}
);
};
class App extends Component {
constructor(props) {
super(props);
this.state = {
signedIn: false,
checkedSignIn: false
};
}
async componentWillMount() {
await isSignedIn()
.then(res => this.setState({ signedIn: res, checkedSignIn: true}))
.catch(err => alert("An error occurred"));
}
render() {
const { checkedSignIn, signedIn } = this.state;
// If we haven't checked AsyncStorage yet, don't render anything (better ways to do this)
if (!checkedSignIn) {
return null;
}
const Layout = createRootNavigator(signedIn);
return (
<SafeAreaView style={styles.safeArea}>
<View style={{flex: 1, backgroundColor: '#ffffff'}}>
<StatusBar barStyle="light-content"/>
<Layout />
<AlertContainer/>
</View>
</SafeAreaView>
)
}
};
export const createRootNavigator=(signedIn=false)=>{
返回开关导航器(
{
签名人:{
屏幕:SignedIn
},
签名输出:{
屏幕:SignedOut
}
},
{
初始路由名称:signedIn?“signedIn”:“SignedOut”
}
);
};
类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
签名:错,
核对签名:错
};
}
异步组件willmount(){
等待伊西涅丁()
.then(res=>this.setState({signedIn:res,checkedSignIn:true}))
.catch(err=>alert(“发生错误”);
}
render(){
const{checkedSignIn,signedIn}=this.state;
//如果我们还没有检查AsyncStorage,请不要渲染任何内容(更好的方法)
如果(!checkedSignIn){
返回null;
}
const Layout=createRootNavigator(signedIn);
返回(
)
}
};
您如何确定传递给createRootNavigator
的signedIn
的值?你能分享一些你调用了createRootNavigator
函数的代码吗。@Prasupal现在请看一看你能在从onSignOut
中删除括号后检查一下吗,比如export const onSignOut=async()=>AsyncStorage.removietem(用户密钥)代码>@PrasunPal它仍然是一样的。问题是,如果我将控制台.log
放在导航.导航(“SignedOut”)
之后,它会被打印出来,但函数未被调用。请检查应用程序中的signedIn
中得到的值。render
函数,控制切换的是signedIn
值。顺便说一句,你能分享一下isSignedIn()的实现吗?
export const createRootNavigator = (signedIn = false) => {
return SwitchNavigator(
{
SignedIn: {
screen: SignedIn
},
SignedOut: {
screen: SignedOut
}
},
{
initialRouteName: signedIn ? "SignedIn" : "SignedOut"
}
);
};
class App extends Component {
constructor(props) {
super(props);
this.state = {
signedIn: false,
checkedSignIn: false
};
}
async componentWillMount() {
await isSignedIn()
.then(res => this.setState({ signedIn: res, checkedSignIn: true}))
.catch(err => alert("An error occurred"));
}
render() {
const { checkedSignIn, signedIn } = this.state;
// If we haven't checked AsyncStorage yet, don't render anything (better ways to do this)
if (!checkedSignIn) {
return null;
}
const Layout = createRootNavigator(signedIn);
return (
<SafeAreaView style={styles.safeArea}>
<View style={{flex: 1, backgroundColor: '#ffffff'}}>
<StatusBar barStyle="light-content"/>
<Layout />
<AlertContainer/>
</View>
</SafeAreaView>
)
}
};