Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

504
Views
navigate('/') doesn't navigate

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>
  );
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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)()}
about 4 years ago · Juan Pablo Isaza Report

0

If logoutUser is meant to be an action, then you need to actually dispatch that action to your redux store. This assumes you have correctly added the thunk (or other similar asynchronous action middleware).

Example:

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

...

import { useDispatch } from 'react-redux';
import { useNavigate } from 'react-router-dom';

const Dashboard = () => {
  const dispatch = useDispatch();
  const navigate = useNavigate();

  return (
    <div>
      <StyledButton
        bg={colors.red}
        to="#"
        onClick={() => dispatch(logoutUser(navigate))}
      >
        Logout
      </StyledButton>
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!