React Router

Subject: React

Routing in React Router v6 (Core Concepts)

React Router helps manage navigation between different pages in a Single Page Application (SPA). It updates the UI without reloading the full page.


βœ… 1. Installation


🧱 2. Basic Setup

🧾 Explanation

TermDescription
BrowserRouterProvides routing context using the browser history API
RoutesWrapper for all Route definitions
RouteMaps a path (URL) to a component (element)
path="*"Wildcard route for handling 404s (NotFound page)

🌐 3. Creating Pages


πŸ”— 4. Navigation Between Pages

ComponentPurpose
LinkNavigates between routes (no page reload)

🧠 Summary (Routing Concepts)

FeatureReact Router v6 Syntax
Routes container<Routes>
Route matchingExact by default
Wildcard routepath="*"
Navigation<Link to="...">
Nested RoutesFully supported

πŸ”„ Part 2: React Router v6 Hooks (useNavigate, useParams, etc.)

Hooks make navigation and route data access easier in functional components.

πŸ“ 1. useNavigate()

Navigate programmatically (e.g., after a button click):


πŸ—ΊοΈ 2. useParams()

Access dynamic route parameters:

Used like:


πŸ“Œ 3. useLocation()

Access info about the current route (URL, search, hash, etc):


πŸ“€ 4. useSearchParams()

Handle query strings (like ?sort=asc&page=2):

URL Example: /products?sort=asc


πŸ”š Wrap-Up

πŸ”— Routing handles:

  • URL to component mapping
  • Navigation links
  • Wildcard route handling (404)
  • Layouts & nested routing

πŸ§ͺ React Router Hooks:

HookPurpose
useNavigate()Redirect programmatically
useParams()Access dynamic route values
useLocation()Get info about the current URL
useSearchParams()Work with query strings

React Router v6 makes routing in React apps simple, powerful, and flexible for both beginners and advanced developers.