liveBook · Manning

useNavigate

As explained in React Hooks in Action: With Suspense and Concurrent Mode

useNavigate is a React Router hook that returns a function for programmatically changing the current URL and navigating to another route.

useNavigate is a React Router hook that returns a function for changing the URL programmatically. Calling that function prompts the router to render the UI associated with the new path, making it useful for actions such as a Next button or group selector. section 10.1.4

Use useNavigate when navigation follows an event rather than a permanently rendered link:

  • Use event handlers for controls such as a group selector or Next button; use Link when rendering direct navigation links. section 10.1.4

Advance to the next bookable

const navigate = useNavigate(); function nextBookable () { const i = bookablesInGroup.indexOf(bookable); const nextIndex = (i + 1) % bookablesInGroup.length; navigate(getUrl(bookablesInGroup[nextIndex].id)); } Here, the URL becomes the selected state: navigating to /bookables/1 or /bookables/2 causes the matching route and view to render.

How React Hooks in Action: With Suspense and Concurrent Mode covers it

How deep it goes
Explains the returned function, direct URL calls, links, reusable URL builders, and a beta-version API caveat.
Examples
Demonstrates bubble-tea navigation, bookable links, group selection, a Next button, and /bookables/{id} destinations.
What it assumes
Assumes function components, event handlers, JSX, React Router routes, and URL path parameters.
What you can do after
Apply useNavigate via React Router to implement programmatic route changes and synchronize selections with URL paths.
How it connects
Connects useNavigate with React Router routes, Link, useParams, URL parameters, event handlers, and URL-backed state.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Hooks in Action: With Suspense and Concurrent Mode. The section links point to the parts of the book it draws on.