Skip to main content
Home
Drupal life hacks

Main navigation

  • Drupal
  • React
  • WP
  • Contact
  • About
User account menu
  • Log in

Breadcrumb

  1. Home

Using React Link for Navigation in a React Application

By admin, 5 April, 2024

Link is a component provided by React Router that allows you to create navigation links in your React application. It's used to navigate between different routes defined in your application without triggering a full page reload.

Here's an example of how to use Link in a React application:

import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';const Home = () => <h2>Home</h2>;
const About = () => <h2>About</h2>;
const App = () => (
 <Router>
   <div>
     <nav>
       <ul>
         <li>
           <Link to="/">Home</Link>
         </li>
         <li>
           <Link to="/about">About</Link>
         </li>
       </ul>
     </nav>
     <Route path="/" component={Home} exact />
     <Route path="/about" component={About} />
   </div>
 </Router>
);

export default App;

In this example, Link components are used to create navigation links to the "Home" and "About" routes. When a user clicks on a Link, the URL is updated using the React Router's navigation mechanism, but the page does not reload completely. Instead, React Router renders the component associated with the new route. This provides a seamless navigation experience within the single-page application.

Tags

  • React

Comments

About text formats

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Powered by Drupal