我对React-native还是相当陌生,但是我已经能够使用firebase进行身份验证登录。除此之外,我还使用了一个抽屉进行进一步的导航。但是,当用户登录后,我希望他们能够通过该抽屉上的按钮注销。目前,当我单击它时,它什么也没做...
我尝试将this.logout函数更改为this.logout(),该函数到达console.log(“ here)语句,但返回未定义的错误。我也尝试过绑定,但没有结果。
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
authenticated: false,
}
this.logout = this.logout.bind(this)
};
componentDidMount() {
firebaseConfig.auth().onAuthStateChanged((authenticated) => {
authenticated ? this.setState(() => ({
authenticated: true,
})) : this.setState(() => ({
authenticated: false,
}));
});
}
//Logout of firebase and navigate to login screen
logout = () => {
console.log("here")
firebaseConfig.auth().signOut()
.then(() => this.props.navigation.navigate('Login'))
.catch((err) => {
console.log(err)
});
};
render() {
//const {currentUser} = this.state
return (
<Application/>
)
}
}
const DrawerComponents = (props) =>
<SafeAreaView style={styles.safeArea}>
<View style={styles.logo}>
</View>
<ScrollView>
<DrawerItems{...props}/>
<View style={{flex: 1}}>
<Button title="Logout" onPress={this.logout}/>
</View>
</ScrollView>
</SafeAreaView>
const TabNavigator = createDrawerNavigator({
Home: {screen: Homepage},
Profile: {screen: Profile},
Login: {
screen: Login,
navigationOptions: ({navigation}) => {
return {
drawerLabel: () => null,
drawerLockMode: 'locked-closed'
}
}
},
SignUp: {
screen: SignUp,
navigationOptions: ({navigation}) => {
return {
drawerLabel: () => null,
drawerLockMode: 'locked-closed'
}
}
},
Loading: {
screen: Loading,
navigationOptions: ({navigation}) => {
return {
drawerLabel: () => null,
drawerLockMode: 'locked-closed'
}
}
},
},
{
headerMode: null,
contentComponent: DrawerComponents,
drawerWidth: WIDTH,
drawerPosition: "right",
initialRouteName: 'Home',
contentOptions: {
activeTintColor: "orange"
}
});
const Application = createAppContainer(TabNavigator)
});
我希望在单击注销后,firebase上当前登录的用户会注销,然后重定向到登录屏幕。