Testing programmatic navigation
Sometimes, you’ll want to trigger a location change programmatically—in other words, without waiting for a user to click a link.
There are two ways to do this: one using the useNavigate
hook, and the second using a history
instance that you pass into your top-level router.
Navigation inside and outside of components
In this chapter, we’ll look at just the first method, using the hook. Later, in Chapter 12, Test-Driving Redux, we’ll use the second method to change the location within a Redux action.
The useNavigate
hook is the appropriate method when you’re able to navigate from within a React component.
In the Appointments application, this happens in two places. The first is after a customer has been added and we want to move the user on to the /addAppointment
route. The second is after that form has been completed and the appointment has been created—then we want to move them back to the default...