navigate('/') doesn't navigate

Michael

I'm using React Router Dom v6. So for my web application, I created a Redux action in userAction called logoutUser, and in view dashboard.jsx, I added a logout button that should navigate me to the home page ('/'). My problem is that clicking on logout does nothing, is my code wrong?

userAction.js:

export const logoutUser = (navigate) => {
    return () => {
        sessionService.deleteSession();
        sessionService.deleteUser();
        navigate('/');
    }
}

dashboard:

import {useNavigate} from 'react-router-dom'

const Dashboard = ({logoutUser}) => {
  const navigate = useNavigate();

  return (
    <div>
      <StyledButton bg={colors.red} to="#" onClick={()=>logoutUser(navigate)}>
        Logout
      </StyledButton>
    </div>
  );
yousoumar

logoutUser is returning a function instead of executing the tasks you want. You either change it to:

export const logoutUser = (navigate) => {  
   sessionService.deleteSession();
   sessionService.deleteUser();
   navigate('/');
}

Or your change your onClick part, to the code below, so you execute the function retuned by logoutUser:

onClick={()=>logoutUser(navigate)()}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

this.router.navigate() doesn't navigate to the URL

Router navigate doesn't save url after navigate

WebView using NavigateToLocalStreamUri doesn't actually Navigate

Why doesn't my anchor navigate in the dropdown?

Backbone router.navigate doesn't redirect

createMaterialTopTabNavigator doesn't navigate after one navigation

Winapi IsDialogMessage() doesn't navigate to multiline edit

Struts2 submit form doesn't navigate to new view

AuthGuard service canActivate doesn't navigate when token refreshed

Cell Click in tableview doesn't navigate to new controller in swift

Angular router doesn't navigate for the first time from component to component

Angular 5 - Routing changes url but doesn't navigate

Router.navigate doesn't work in angular app

Angular 7 navigate is changing URL accordingly BUT doesn't load the component

Why my router navigate function doesn't work in angular 8?

Bottom navigation doesn't navigate when item is clicked

Trying to navigate from a component that doesn't have the navigation props

Navigate in react router 6 doesn't redirect to another page

Loader is stuck and doesn't navigate to next page - Flutter

Can't find variable: navigate

why can't navigate to Downloads

with Rselenium, can't navigate anymore

App can't Navigate to MainPage

INavigationAware can't navigate away

can't navigate SimpleXMLElement data

Can't navigate to other component

Using React.js lazy loading when I navigate to a nested route the main bundle doesn't load

Why doesn't Vue close websocket connections when i navigate to other pages?

How to navigate the parse tree when the scraped content doesn't match the web page content