MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Programmatic Navigation and useSearchParams0%
Study Materials
React.js for IntermediateProgress 0%
1.1Component Reusability and Polymorphic Components1.2Render Props Pattern in Modern React1.3Higher-Order Components vs Modern Hooks1.4Compound Components Pattern Explained
4.1Client-Side Routing Architecture with React Router4.2Defining Routes, Route Layouts, and Outlets4.3Dynamic URL Parameters with useParams4.4Programmatic Navigation and useSearchParams
HomeTutorialsReact.js for IntermediateProgrammatic Navigation and useSearchParams
The Prop Drilling Dilemma and Context Solution

Programmatic Navigation and useSearchParams

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. What is Programmatic Navigation?

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:

  • Redirecting a user to their dashboard immediately after a successful login.
  • Navigating to an order confirmation screen after completing a checkout payment.
  • Redirecting an unauthorized user back to the sign-in page.

In React Router v6+, programmatic navigation is executed using the useNavigate hook.

1import React from 'react';
2import { useNavigate } from 'react-router-dom';
3 
4export default function LoginForm() {
5 const navigate = useNavigate();
6 
7 const handleLogin = async (e) => {
8 e.preventDefault();
9 const token = await authenticateUser();
10 
11 if (token) {
12 // Programmatically navigate to dashboard route
13 navigate('/dashboard');
14 
15 // Or replace history entry (user cannot click browser "Back" into login screen)
16 // navigate('/dashboard', { replace: true });
17 }
18 };
19 
20 return <form onSubmit={handleLogin}><button type="submit">Log In</button></form>;
21}

Delta Navigation (Back & Forward)

useNavigate also allows navigating through browser history:

1navigate(-1); // Equivalent to clicking browser "Back" button
2navigate(1); // Equivalent to clicking browser "Forward" button

2. Managing Query Strings with useSearchParams

URL Query Strings (search parameters) are the key-value pairs appended after a ? in the URL:

1/courses?category=frontend&sort=rating&page=2

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:

  1. 1
    searchParams: A modern URLSearchParams object to read current query parameters.
  2. 2
    setSearchParams: An updater function to modify URL parameters programmatically.
1import React from 'react';
2import { useSearchParams } from 'react-router-dom';
3 
4export default function SearchableCatalog() {
5 const [searchParams, setSearchParams] = useSearchParams();
6 
7 // 1. Read query parameters (defaults provided if missing)
8 const category = searchParams.get('category') || 'all';
9 const sort = searchParams.get('sort') || 'popular';
10 
11 // 2. Update query parameters in URL
12 const handleCategoryChange = (newCategory) => {
13 setSearchParams({
14 category: newCategory,
15 sort: sort // Preserve other query parameters
16 });
17 };
18 
19 return (
20 <div className="catalog-filters">
21 <h3>Active Category: {category}</h3>
22 
23 <div className="btn-group">
24 <button onClick={() => handleCategoryChange('frontend')}>Frontend</button>
25 <button onClick={() => handleCategoryChange('backend')}>Backend</button>
26 <button onClick={() => handleCategoryChange('data')}>Data Science</button>
27 </div>
28 </div>
29 );
30}

3. Why Query Params Beat Component State for Search

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:

  • The URL becomes /courses?category=frontend&sort=rating.
  • Users can bookmark the page, refresh the browser, or share the link with colleagues, and the exact same filtered state will load immediately!

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What hook in React Router v6 is used for programmatic navigation?
Question 2
How do you simulate clicking the browser's native "Back" button using useNavigate?
Question 3
What option should be passed to navigate('/dashboard', { replace: true }) and why?
Question 4
What hook is used to read and update URL query strings (e.g. ?category=react&page=1) in React Router?
Question 5
Why is storing search and filter criteria in URL search parameters preferable to storing them in local component useState?
Next Lesson

The Prop Drilling Dilemma and Context Solution

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Dynamic URL Parameters with useParamsThe Prop Drilling Dilemma and Context Solution

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext