Nearby lessons

29 of 43

🛣️ ReactJS – Programmatic Navigation

📌 What is Programmatic Navigation?

Navigating Programmatically Using React Router

Programmatic navigation means changing routes using code instead of links. For example, using navigate('/home') inside a function to go to the Home page after a button click or form submission.

This makes navigation dynamic and event-driven, useful in many real-world apps.

📌 Objectives

  • Navigation on button click
  • Navigation inside event handler
  • Navigation to multiple pages with single event handler
  • Condition based navigation
  • Go Back Button

➡️ Navigation on Button Click

We can use useNavigate() from react-router-dom and call it inside a button's onClick event.

Example03
Loading editor…

⚙️ Navigation inside Event Handler

We can define navigation logic inside functions and reuse them across multiple buttons.

Example04
Loading editor…

🔗 Navigation to Multiple Pages with Single Handler

We can create a reusable function that accepts a url argument and navigates accordingly.

Example05
Loading editor…

⚖️ Condition Based Navigation

We can use conditions to decide which route to navigate to.

Example06
Loading editor…

↩️ Go Back Button

React Router also allows navigating back using navigate(-1).

Example07
Loading editor…

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4