我正在尝试注销用户,然后将其发送到SignedOut屏幕但是当我按下Sign Out时它正在调用unauthUser
功能但是然后它将交换机导航器重新启动到仪表板而我必须进入配置文件屏幕再次点击退出登出。知道我在这一切中做错了什么吗?
这是来自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>
来自Auth.js的 onSignOut()
export let USER_KEY = 'myKey';
export const onSignIn = async () => { await AsyncStorage.setItem(USER_KEY, 'true') };
export const onSignOut = async () => { await AsyncStorage.removeItem(USER_KEY) };
export const isSignedIn = () => {
return new Promise((resolve, reject) => {
AsyncStorage.getItem(USER_KEY)
.then(res => {
if (res !== null) {
// console.log('true')
resolve(true);
} else {
resolve(false);
// console.log('false')
}
})
.catch(err => reject(err));
});
};
来自authActions.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>
)
}
};