2

I am working on a React app where I am trying to navigate from a loginform to the homescreen of my app using an onSubmit function. This is what I have so far. In my formik form im trying to pass the email value when the form gets submitted. After that the user gets routed to '/dashboard' and ends up in '/dashboard/app' where im trying to get the email value logged in the console. I tried logging route.params and route.email but this gives either undefined or errors.

enter image description here

enter image description here

enter image description here

John
  • 97
  • 1
  • 7

2 Answers2

6

Did you mean to add state to the navigation?

Original:

onSubmit: () => {
          navigate('/dashboard', {replace:true, email: values.email})
}

Changed to (with state):

onSubmit: () => {
          navigate('/dashboard', {replace:true,
                                  state: { 
                                           email:values.email
                                          }
                                   })
}
0

As far as I understand, you want to redirect with some params. The easiest solution is to use "history push" with the state.

import { useHistory } from "react-router-dom";

const LoginForm = () => {
  const history = useHistory();
  const formik = useFormik({
    onSubmit: () => {
      history.push('/dashboard', {replace: true, email: values.email})
    }
  })
}

In a similar manner access history state by: history.state

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
  • Im using the useNavigate hook from react-router-dom. Do you know how to implement it with useNavigate? – John Sep 30 '21 at 10:43