Skip to main content

React Router Introduction in React Routing

React Router is a popular and widely used library for handling routing in React applications. It allows you to create single-page applications with navigation, manage the URL, and render components based on the current URL. This tutorial will provide an introduction to React Router and demonstrate how to set up basic routing in a React application.

Installing React Router

To start using React Router in your project, you need to install the required packages. You can do this by running the following command in your project folder:

npm install react-router-dom

Basic Routing Example

Now, let's create a simple React application with three components, Home, About, and Contact, and set up basic routing using React Router.

First, let's create the three components:

JavaScript
// Home.js
import React from 'react';

function Home() {
return <h1>Home Page</h1>;
}

export default Home;

// About.js
import React from 'react';

function About() {
return <h1>About Page</h1>;
}

export default About;

// Contact.js
import React from 'react';

function Contact() {
return <h1>Contact Page</h1>;
}

export default Contact;

Next, let's set up the routing using React Router. We'll import the necessary components from react-router-dom and wrap our main App component with the BrowserRouter component. Then, we'll use the Route component to define the routes for our application.

JavaScript
// App.js
import React from 'react';
import { BrowserRouter, Route } from 'react-router-dom';
import Home from './Home';
import About from './About';
import Contact from './Contact';

function App() {
return (
<BrowserRouter>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</BrowserRouter>
);
}

export default App;

In the App component, we've defined three routes:

  1. The home page at the root path ("/") with the exact prop to match only when the path is exactly "/".
  2. The about page at the "/about" path.
  3. The contact page at the "/contact" path.

Now, when you navigate to these paths in your browser, React Router will render the corresponding components.

Finally, let's add some navigation links to our application using the Link component from react-router-dom.

JavaScript
// App.js
import React from 'react';
import { BrowserRouter, Route, Link } from 'react-router-dom';
import Home from './Home';
import About from './About';
import Contact from './Contact';

function App() {
return (
<BrowserRouter>
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/contact">Contact</Link>
</nav>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</BrowserRouter>
);
}

export default App;

With these navigation links, users can now easily navigate between the different pages of your application.

Conclusion

React Router is a powerful library for managing routing in React applications. In this tutorial, we have introduced the basic concepts of React Router, such as installing the necessary packages, creating routes, and adding navigation links. With these foundations, you can now begin to explore more advanced features of React Router, like nested routes, route parameters, and redirects.

Remember to keep practicing and experimenting with different routing scenarios to become more comfortable with React Router and its various capabilities. As you continue to develop your React applications, you'll find that React Router is an essential tool for creating a seamless user experience.