DEV Community

useNavigate tutorial React JS

Saleh Mubashar on October 28, 2021

Hi guys !. In this post we will learn how to use the useNavigate hook in React JS. useNavigate is part of React Router and has replaced useHistory...
Collapse
 
vishma profile image
Vishma Pratim Das

Hey,
so according to react router documentation, useNavigate() returns a method that allows us to navigate from one link to another. We don't store it inside the navigate variable. It is a hook. It is not a regular function. When we assign useNavigate() to navigate, the hook gets called and the method gets returned, which is stored inside it.

Collapse
 
fahadashiq12 profile image
Fahad Ashiq

Very Useful.

Collapse
 
nvgrae profile image
nvg-rae

Thanks for bearing with a newbie comment! I used to be able to just do this.props.history.push(/Welcome) in my class component to go to a new route from the click handler in a class component. Now I need to create an entire functional component just to use that "extremely useful and easy to use" function? Any tips on how to handle that in a class component are greatly appreciated.

Collapse
 
salehmubashar profile image
Saleh Mubashar • Edited

Hi
Ok so the problem is that you can not use this prop in class components because the children components must use react hooks to access the route context which is only possible with functional components.
Check this out: stackoverflow.com/questions/647829...

Collapse
 
nvgrae profile image
nvg-rae

Thank you for taking a look, Saleh!

Thread Thread
 
salehmubashar profile image
Saleh Mubashar • Edited

no problem :).
if you have any questions, you can always reach out to me on twitter
cheers :)

Collapse
 
bennett1412 profile image
Bennett B Madavana

Thanks for the article,
Is it okay to use a button instead of an anchor tag when you're redirecting to another page? won't it cause accessibility issues?

Collapse
 
pierre1590 profile image
Piero Sabino

Hi, I have a problem, watching a video on youtube that used history.push ('/ login? redirect = shipping'), how can I modify it using navigate, because if I use the string "navigate ('/ login? redirect = shipping') even if logged in it returns to home page and does not go to the shipping page.

Collapse
 
siyandavid profile image
siyan_david

Sorry did you figure it out im having same issue

Collapse
 
loopmode profile image
Jovica Aleksic • Edited

Please note that you can NOT use a hook in class-based components, as is stated here. Functional components only.

Collapse
 
salehmubashar profile image
Saleh Mubashar • Edited

Yes you are right, i forgot to mention that

Collapse
 
darneltaphil profile image
darneltaphil

Can I use useNavigate to send props to the page I am redirecting to?

Collapse
 
omoluabii profile image
Hammed A

Yes, you can pass props

Collapse
 
cgitosh profile image
Clement Gitonga

What if you want to pass data to the next page? for example select a record from a grid then open edit page passing the selected data

Collapse
 
omoluabii profile image
Hammed A • Edited

Yes, you can pass data
navigate("/todos", { state: { newId: id } })

Collapse
 
abssaxon profile image
Abhinav Saxena

How do we get this state on the receiving end?

Collapse
 
abssaxon profile image
Abhinav Saxena

How do we pass state with useNavigation and receive it on the other end?

Collapse
 
bapangit profile image
bapangit

Want to delete '/login' and navigate to '/profile' after login has successful. How can I delete current '/login' using useNavigate() ?