Skip to main content

Command Palette

Search for a command to run...

Router: Client-Side Routing Made Easy

Published
4 min read
Router: Client-Side Routing Made Easy

REACT ROUTER

React Router is a popular library for implementing routing in React applications. It allows you to define how your application's URL should look and how different components should be rendered based on the current URL. Here's a brief overview of how to use React Router in a React application:

  1. Install React Router using npm or yarn

     npm install react-router-dom
    
  2. Import the necessary components from React Router:

     import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
    
  3. Wrap your entire application in the <Router> component:

     <Router>
       ... your app code goes here ...
     </Router>
    
  4. Define your routes using the <Route> component:

     <Route exact path="/" component={Home} />
     <Route path="/about" component={About} />
     <Route path="/contact" component={Contact} />
    

In this example, we define three routes: one for the home page, one for the About page, and one for the contact page. The the exact prop is used on the home page route to ensure that it only matches the exact URL path of "/", while the other routes use the path prop to define their URLs.

  1. Add navigation links using the <Link> component:

     <Link to="/">Home</Link>
     <Link to="/about">About</Link>
     <Link to="/contact">Contact</Link>
    
  2. Finally, render your application:

     ReactDOM.render(<App />, document.getElementById('root'));
    

When the user clicks on a navigation link, React Router will match the URL to the appropriate <Route> component and render the corresponding component. The component prop specifies which component to render for each route, and you can also use the render prop to render inline JSX instead of a component. Additionally, you can use other React Router components like <Switch> and <Redirect> to implement more advanced routing features.

Here's an example of how you can use React Router to implement routing in a simple React application:

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';

// Define your components
const Home = () => <h1>Welcome to the Home page!</h1>;
const About = () => <h1>About Us</h1>;
const Contact = () => <h1>Contact Us</h1>;

// Define your routes
const App = () => (
  <Router>
    <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>

      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/contact" component={Contact} />
    </div>
  </Router>
);

// Render your app
ReactDOM.render(<App />, document.getElementById('root'));

In this example, we first import the necessary components from React Router, including the <Router>, <Route>, and <Link> components. We then define three simple components for our Home, About, and Contact pages.

Next, we wrap our entire application in the <Router> component and define our routes using the <Route> component. We also add navigation links using the <Link> component.

Finally, we render our application using ReactDOM.render().

When the user clicks on a navigation link, React Router will match the URL to the appropriate <Route> component and render the corresponding component. For example, if the user clicks on the "About" link, React Router will match the URL to the /about path and render the <About> component.

REACH ROUTER

Reach Router is a popular routing library for React that provides a simple and flexible way to handle client-side routing in your React applications. It's similar to React Router but with a few differences in API and functionality.

To use Reach Router, you first need to install it:

npm install @reach/router

Then, you can use it in your React components:

import React from 'react';
import { Router, Link } from '@reach/router';

// Define your components
const Home = () => <h1>Welcome to the Home page!</h1>;
const About = () => <h1>About Us</h1>;
const Contact = () => <h1>Contact Us</h1>;

// Define your routes
const App = () => (
  <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>

    <Router>
      <Home path="/" />
      <About path="/about" />
      <Contact path="/contact" />
    </Router>
  </div>
);

// Render your app
ReactDOM.render(<App />, document.getElementById('root'));

In this example, we first import the necessary components from Reach Router, including the <Router> and <Link> components. We then define three simple components for our Home, About, and Contact pages.

Next, we add navigation links using the <Link> component and define our routes using the <Router> component. We use the path prop to define the URL paths for each of our routes and specify which component should be rendered for each route.

Finally, we render our application using ReactDOM.render().

When the user clicks on a navigation link, Reach Router will match the URL to the appropriate component and render it. For example, if the user clicks on the "About" link, Reach Router will match the URL to the /about path and render the <About> component.

More from this blog

Asritha's Blog

40 posts