Welcome! Today, we'll journey through the process of programmatic navigation using React Router v6. We'll explore the hooks useNavigate
and useLocation
, along with a concept known as navigation with state. Let's jump in!
Typically, a React app that uses Router v6 utilizes Link
or NavLink
for navigation. However, there are instances when we require more control, such as redirecting after a form submission. That's when we turn to programmatic navigation.
useNavigate
hook provides a function enabling navigation in our React router application. In addition to navigating to absolute paths like /login
or /home
, useNavigate
possesses the superpower to navigate relative to the current location. It also introduces a new way to navigate backwards.
For instance, if we wanted to move back one step in our route history, we could call . It's a simple and intuitive feature that can be incredibly handy in certain scenarios.
