React Router

🚦 React Router (Complete Beginner-Friendly Guide)

React Router is a library used to handle routing/navigation in React applications. It allows you to move between pages or components without reloading the browser, making apps fast and seamless like SPAs (Single Page Applications).


📦 Install React Router

Run this command in your React project:



 


🧭 Basic Setup

Modify your App.js to include router components:


 


🔗 Navigation with <Link />

Use <Link> instead of <a> to avoid page reloads.


 


🚀 Example Pages

📄 Home.js



 

📄 About.js



 

📄 Contact.js



 


❓ 404 Page (Not Found Route)



 


➡️ Programmatic Navigation (useNavigate())

Navigate users using JavaScript instead of a link.


 


🎯 Route Parameters (Dynamic Routes)

Useful for pages like profiles, product details, etc.

Create a dynamic route:



 

Retrieve parameter:


 


📩 Passing Data with Query Params

Use:



 

Get value via:


 


🧱 Nested Routes

Used for dashboards or sections.


 


⏳ Redirects Using <Navigate />



 


🧠 Summary Table

Feature Supported
Multiple Pages
Dynamic Routes
Redirects
Nested Routing
Programmatic Navigation
Query Parameters

🎉 You Now Know React Router!

You may also like...