Getting undefined value from navigation - React.js

Viewed 40

I am getting undefined value when passing the value through navigate.

Code to send a value to home Component

if(result.status === 200)
        {
            if(user.work === "Worker")
            {
                navigate("/home", {
                    mail:user.email
                });
            }
            
        }

Routing Code

<Router>
      <Routes>
        <Route path="/home" element={<Home />} />
      </Routes>
    </Router>

Home Component

const { location } = useLocation();
console.log(location);
1 Answers

You should send the state in the navigate like this:

if (result.status === 200) {
  if (user.work === "Worker") {
    navigate("/home", {
      state: { mail: user.email },
    });
  }
}

and then you can see the state in the Home component:

const { state } = useLocation();
console.log(state);
Related