Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Routing with React Router

Routing with React Router

Routing is a crucial part of building single-page applications (SPAs) with React.js. React Router is a popular library for handling routing in React applications. It allows you to create navigation between different views or components while maintaining a single-page experience. Here's an overview of how to use React Router for routing in your React application:

1. Installing React Router:

  • You need to install the React Router library using npm or yarn. There are different packages available, but the most common one is react-router-dom, which is used for web applications.
npm install react-router-dom
    

 

 

2. Setting Up Routes:

  • In your React application, you define the routes you want to use. Typically, this is done in your main application file (e.g., App.js).
  • Import the necessary components from react-router-dom, such as BrowserRouter, Route, and Switch.

 

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

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

 

3. Route Configuration:

  • Each <Route> component takes two important props: path (the URL path) and component (the React component to render when the path matches).

4. <Switch> Component:

  • The <Switch> component renders the first <Route> that matches the current location. This ensures that only one route is rendered at a time.

5. Navigation Links:

  • To create navigation links, use the <Link> component from react-router-dom. It allows you to define clickable links that navigate to different routes without causing a full page reload. 

 

import { Link } from 'react-router-dom';

function Navigation() {
  return (
    <nav>
      <ul>
        <li>
          <Link to="/">Home</Link>
        </li>
        <li>
          <Link to="/about">About</Link>
        </li>
        <li>
          <Link to="/contact">Contact</Link>
        </li>
      </ul>
    </nav>
  );
}

 

6. Route Parameters:

  • React Router allows you to define route parameters in your URL paths, which can be accessed as props in your component. For example:
jsx

 

<Route path="/user/:id" component={User} />
       

 

7. Redirects:

  • You can use the <Redirect> component to programmatically redirect users to another route.
import { Redirect } from 'react-router-dom';

function ProtectedRoute({ loggedIn }) {
  if (!loggedIn) {
    return ;
  }
  return ;
}
    

 

 

8. Route Guards:

  • Route guards, also known as middleware, can be implemented by wrapping your components with higher-order components (HOCs) or using inline functions to check conditions before rendering a route.
function PrivateRoute({ component: Component, loggedIn, ...rest }) {
  return (
    
        loggedIn ?  : 
      }
    />
  );
}
     

 

 

9. Nested Routes:

  • React Router supports nested routes, allowing you to render child routes within a parent route.
<Route path="/dashboard" component={Dashboard}>
  <Route path="/dashboard/profile" component={Profile} />
  <Route path="/dashboard/settings" component={Settings} />
</Route>
     

 

With React Router, you can create complex navigation structures and manage routing in your React application easily. It provides a seamless and efficient way to handle routing within single-page applications.