Hello there, fellow coding enthusiast! Welcome to the wonderful world of React Router. If you’re just getting started with building web applications using React, understanding how to navigate between pages or components is a crucial skill. React Router is a powerful library that aids in defining these navigational routes in your app, making it feel like a fully-fledged single-page application (SPA).
By the end of this article, you’ll be equipped with the basics of using React Router for effective navigation in your React apps. Ready to get started? Let’s dive in!
What is React Router?
Before we jump into coding, let’s briefly cover what React Router is. React Router is a standard library for routing in React. It enables you to make navigation among views of various components in a React application seamless. With React Router, you can manage URL changes, route settings, and handle dynamic navigation with ease.
Setting Up Your React Project
First things first, if you haven’t already set up a React project, let’s do that quickly:
- Install Node.js if it’s not already on your machine.
- Open your terminal/command prompt.
- Use
npxto create a new React app:
npx create-react-app react-router-tutorial
Navigate into your project directory:
cd react-router-tutorial
Installing React Router
React Router isn’t included by default in a new React project, so you’ll need to install it:
npm install react-router-dom
This command will add react-router-dom (the library specifically for web applications) to your project’s dependencies.
Basic Setup of React Router
Step 1: Organizing Your Components
To understand routing, let’s create a simple structure with three components: Home, About, and Contact.
Create a folder named components inside the src directory, and add three files:
Home.jsAbout.jsContact.js
Home.js
import React from 'react';
const Home = () => {
return (
<div>
<h1>Home Page</h1>
<p>Welcome to the Home Page!</p>
</div>
);
};
export default Home;
About.js
import React from 'react';
const About = () => {
return (
<div>
<h1>About Page</h1>
<p>This is all about us.</p>
</div>
);
};
export default About;
Contact.js
import React from 'react';
const Contact = () => {
return (
<div>
<h1>Contact Page</h1>
<p>Reach out to us here.</p>
</div>
);
};
export default Contact;
Step 2: Setting Up the Router
Open your index.js file and update it like this:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>,
document.getElementById('root')
);
Step 3: Defining Routes
Now update App.js to define and use the routes:
import React from 'react';
import { Routes, Route, Link } from 'react-router-dom';
import Home from './components/Home';
import About from './components/About';
import Contact from './components/Contact';
function App() {
return (
<div>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/contact">Contact</Link></li>
</ul>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</div>
);
}
export default App;
Understanding the Code
- BrowserRouter: Enables routing in the React application.
- Link: Allows page navigation without refreshing.
- Routes and Route: Define the structure of URL paths and their corresponding components.
Wrap-Up
Congratulations! 🎉 You’ve just set up a basic routing system in your React app using React Router. This is foundational knowledge for building more complex applications.
Practice Ideas
- Add a new “Services” page and route.
- Style the navigation menu with CSS.
- Explore nested routes for hierarchical navigation.
I hope you found this guide helpful! Remember, every expert started just like you—curious and persistent. Happy coding! 🚀
Note: When not using React Router’s <Link>, make sure to manage href values based on your routing strategy.
