📅  最后修改于: 2023-12-03 15:03:09.082000             🧑  作者: Mango
NavLink is a component in React Router that provides a navigation link which updates the URL in the address bar and adds an active CSS class to the rendered element when it matches the current URL.
The basic syntax of using NavLink component is as follows:
<NavLink to="/path" activeClassName="active">{linkText}</NavLink>
NavLink component accepts the following props:
to
(string or object)
activeClassName
(string)
isActive
(function)
exact
(bool)
true
, the active CSS class will only be applied if the current URL exactly matches the link's destination.strict
(bool)
true
, the active CSS class will also be applied if the current URL is a prefix of the link's destination.location
(object)
Here's an example of using NavLink to create navigation links in a React application:
import { NavLink } from 'react-router-dom';
function Navigation() {
return (
<nav>
<NavLink to="/" exact activeClassName="active">Home</NavLink>
<NavLink to="/about" activeClassName="active">About</NavLink>
<NavLink to="/contact" activeClassName="active">Contact</NavLink>
</nav>
);
}
In this example, clicking on the links will update the URL and add the "active" class to the currently active link.