0

I have a thunk action that logout the user

inside the logout thunk action I have this: dispatch(push('/login'));

I would like to redirect the user with a "refresh" to logout in order to refresh the page and. "clean' the state

also, I would like to clean the redux store, and not its not clean

import { push } from 'connected-react-router';


export function logout() {
  return async (dispatch: any, state: State, services: Services) => {
    try {
        await services.auth.logout();
    } catch (e) {
      services.logger.error(e);
    } finally {
      dispatch({ type: AUTH_LOGOUT });
      dispatch(push('/login'));
    }
  };
}

also when click on a button from component I have

const Logout = () => {
  const dispatch = useDispatch();

  React.useEffect(() => {
    dispatch(logout());
  }, [dispatch]);

  return null;
};

Reducer

   case AUTH_LOGOUT: {
        draft.authenticated = false;
        draft.currentUser = {} ;
        return draft;
      }

Tuz
  • 1,810
  • 5
  • 29
  • 58

3 Answers3

1

Then you can simply do window.location.href = '/login';

andriusain
  • 1,211
  • 10
  • 18
  • also, I would like to clean the redux store, and not its not clean how do I change the routing state? case AUTH_LOGOUT: { draft.authenticated = false; draft.currentUser = {} ; return draft; } this my reduer – Tuz Jun 24 '20 at 05:47
  • the code I gave you will trigger a full page refresh and the state should clean-up in the process – andriusain Jun 24 '20 at 05:55
0

React automatically reloads when you change a state. So you need to change a state. For example a login/logout flag or an authorization flag you have used. From Your code, you need to change it where you have defined your case for AUTH_LOGOUT in your reducer.

Mohinder singh
  • 112
  • 1
  • 6
0

You could use javascript's window object for refresh. e.g., window.location.href = "router_address"

Piyush Rana
  • 631
  • 5
  • 8