How do you create a clickable link to navigate between pages using React Router?
Short answer
Instead of a regular HTML anchor tag, which would cause a full page reload, React Router provides a Link component that changes the current page smoothly using client side routing, while still looking and working like a normal link to the user.
Advertisement
AModel answer
Instead of a regular HTML anchor tag, which would cause a full page reload, React Router provides a Link component that changes the current page smoothly using client side routing, while still looking and working like a normal link to the user.
import { Link } from 'react-router-dom';
function Navbar() {
return (
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
</nav>
);
}Real-world example
A website's navigation menu uses Link components instead of regular anchor tags, so clicking between pages feels instant and smooth instead of triggering a full page reload every time.
Likely follow-ups
- What happens if you accidentally use a regular anchor tag instead of Link for internal navigation?
- How does the NavLink component differ from a regular Link?
Related topics