该问题的解决方法是手动重定向到应用程序主页。下面的代码演示了如何使用 React Navigation 在重定向后返回主页。
import { useNavigation } from '@react-navigation/native';
function LogoutButton() { const navigation = useNavigation();
function handleLogout() { // 假设使用了React Native的AsyncStorage库 AsyncStorage.removeItem('userToken').then(() => { navigation.reset({ index: 0, routes: [{ name: 'Home' }], }); }); }
return ( ); }
在上面的例子中,当用户点击“Logout”按钮时,AsyncStorage将删除用户的令牌(或任何其他信息,您需要在登出时清除的任何信息)。在删除后,使用navigation.reset()方法将导航堆栈重置为只包含主页的路由,以便用户返回到应用程序的起始页面。