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.
Comments