How to make redirect after cleaning localstorage?

Viewed 814

How, after cleaning the local storage, redirect to the desired page? Now a problem occurs, the user presses the button - Exit, localalstorage is cleared, but the redirection does not occur.

const onHandleSignOut = () => {
    localStorage.removeItem('session');
    return <Redirect to="/auth" />
}

<li className="header__mainmenu-item" onClick={() => onHandleSignOut()}>
    <FaSignOutAlt />
</li>
3 Answers
this.props.history.push("/auth");

hope it will helpful for you...

You can redirect directly using window.location.href = '/ulr_to_redirect';

you can create a component to make redirection after cleaning storage :

import { NavigationEvents } from 'react-navigation';
class Logout extends Component {

   _clearAsyncStorage = async () => {
     AsyncStorage.clear();
    };

    render() {
        const { navigate } = this.props.navigation;
         return (
         <View>
            <Text>Logout</Text>
              <NavigationEvents onDidFocus={() => navigate('Login')} />
            </View>
         );
    }
 }

Make sur that you have created your navigation inside the navigation component :

you can use react navigation v4 or v5 to create a stack navigation :

For me a added the logout link inside a drawer :

const AppDrawerNavigator = createDrawerNavigator(
 .... other components 
  Déconnexion: {
  screen: Logout,
  navigationOptions: {
    drawerIcon: ({ tintColor }) => (
      <Icon
        name={Platform.OS === 'ios' ? 'ios-exit' : 'md-exit'}
        size={30}
        style={styles(tintColor).drawerIcons}
        type="material"
      />
    ),
  },
)
Related