Declarative navigation using <Link> and <NavLink> works when a user clicks a visible hyperlink. However, real-world applications frequently require navigation in response to code execution:
In React Router v6+, programmatic navigation is executed using the useNavigate hook.
useNavigate also allows navigating through browser history:
useSearchParamsURL Query Strings (search parameters) are the key-value pairs appended after a ? in the URL:
Query strings are ideal for shareable UI states: filters, search terms, pagination, and sorting.
React Router provides useSearchParams, which functions similarly to React's useState, returning:
searchParams: A modern URLSearchParams object to read current query parameters.setSearchParams: An updater function to modify URL parameters programmatically.If an e-commerce catalog stores filters in local useState, when a student finds a great set of courses and copies the URL to send to a friend, the filters are lost because the URL only says /courses.
When filters are stored in searchParams:
/courses?category=frontend&sort=rating.useNavigate?navigate('/dashboard', { replace: true }) and why??category=react&page=1) in React Router?useState?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Dynamic URL Parameters with useParams | The Prop Drilling Dilemma and Context Solution |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.