3

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>
  • 1
    Does this answer your question? [How to redirect in React](https://stackoverflow.com/questions/46679318/how-to-redirect-in-react) – Yehor Androsov Feb 29 '20 at 12:34
  • Does this answer your question? [Programmatically navigate using react router](https://stackoverflow.com/questions/31079081/programmatically-navigate-using-react-router) – keikai Feb 29 '20 at 12:36

3 Answers3

0
this.props.history.push("/auth");

hope it will helpful for you...

Pavan Nagadiya
  • 652
  • 4
  • 10
0

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

0

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"
      />
    ),
  },
)
Khaled Boussoffara
  • 1,567
  • 2
  • 25
  • 53