The react-router-dom library provides several components for defining and navigating between routes in a React application. Here are some of the most commonly used components and their purposes:
BrowserRouter: A component that provides a router for your application. You typically wrap your entire application with BrowserRouter to enable routing in your application.
Route: A component that defines a specific route in your application. You specify the path and component to render when the route is matched.
Switch: A component that wraps a set of Route components and renders only the first matching route. This ensures that only one component is rendered at a time, even if multiple routes match the URL.
Link: A component that provides a clickable link to another route in your application. By using Link, you can avoid full page refreshes when navigating between routes.
NavLink: A component that is similar to Link, but allows you to add custom styling and functionality to the active link when it is selected by the user.
Redirect: A component that redirects the user to another route when a specific condition is met. For example, you can use Redirect to redirect the user to a login page if they are not authenticated.
Here's an example of how to use react-router-dom to implement a simple client-side routing system:
import React from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
function Home() {
return (
<div>
<h1>Welcome to the Home page!</h1>
<Link to="/about">Go to About page</Link>
</div>
);
}
function About() {
return (
<div>
<h1>About page</h1>
<Link to="/">Go to Home page</Link>
</div>
);
}
function App() {
return (
<Router>
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/about">
<About />
</Route>
</Switch>
</Router>
);
}
export default App;
In this example, we use the BrowserRouter component to wrap our App component and provide the routing context. We then use the Switch component to render either the Home or About component based on the current URL. Finally, we use the Link component to create clickable links that navigate between the different routes.
react-router-dom is a powerful library that can help you create more complex and dynamic applications with React. By using client-side routing, you can create a smoother and more seamless user experience, without requiring a full page refresh for every page or route change.
Link and useHistory are two of the most commonly used components/hooks in the react-router-dom library.
Link is a component that allows you to create links to other routes in your application. By using Link, you can avoid full page refreshes when navigating between pages, and create a smoother and more seamless user experience.
Here's an example of how to use Link to create a link to another route:
import React from 'react';
import { Link } from 'react-router-dom';
function Home() {
return (
<div>
<h1>Welcome to the Home page!</h1>
<Link to="/about">Go to About page</Link>
</div>
);
}
In this example, we use Link to create a clickable link that navigates to the /about route in our application. By using Link instead of a regular anchor tag, we can avoid a full page refresh and maintain the state of our application.
useHistory is a hook that provides access to the browser history API. By using useHistory, you can programmatically navigate to other routes, go back and forward in the browser history, and manage the browser location.
Here's an example of how to use useHistory to programmatically navigate to another route:
import React from 'react';
import { useHistory } from 'react-router-dom';
function MyComponent() {
const history = useHistory();
function handleClick() {
history.push('/about');
}
return (
<div>
<button onClick={handleClick}>Go to About page</button>
</div>
);
}
In this example, we use useHistory to get a reference to the browser history API. We then use the push method to programmatically navigate to the /about route when the user clicks on the button.
By using Link and useHistory, you can create a dynamic and responsive user interface in your React application, without requiring full page refreshes or server-side rendering.
NavLink is a component from the react-router-dom library that allows you to create links to other routes in your application with additional styling and functionality. By using NavLink, you can add custom styling to the active link when it is selected by the user.
Here's an example of how to use NavLink to create a styled navigation menu:
import React from 'react';
import { NavLink } from 'react-router-dom';
import './Navigation.css';
function Navigation() {
return (
<nav>
<ul>
<li>
<NavLink exact to="/" activeClassName="active">
Home
</NavLink>
</li>
<li>
<NavLink to="/about" activeClassName="active">
About
</NavLink>
</li>
<li>
<NavLink to="/contact" activeClassName="active">
Contact
</NavLink>
</li>
</ul>
</nav>
);
}
In this example, we use NavLink to create clickable links to the different routes in our application. We use the exact prop on the Home link to indicate that it should only be active when the URL matches exactly, and we use the activeClassName prop to specify the name of the CSS class to apply when the link is active.
To style the active link, you'll need to define the CSS class in a separate stylesheet. Here's an example of what the Navigation.css file might look like:
.active {
font-weight: bold;
color: red;
}
In this example, we define the active CSS class with a bold font weight and red color. This class will be applied to the active NavLink link when it is selected by the user.
By using NavLink, you can create a custom styled navigation menu with minimal effort, and provide visual feedback to the user when they select a link.
RouterProvider and createBrowserRouter are react-router-dom v6 library.
RouterProvider: This is a component that allows you to define your own routing logic using a custom router. The idea behind RouterProvider is that you can use any router library that you like, rather than being restricted to the routers provided by react-router-dom.
Here's an example of how to use RouterProvider with the universal-router library:
import React from 'react';
import { RouterProvider } from 'react-router5';
import createRouter from 'router5';
const routes = [
{ name: 'home', path: '/' },
{ name: 'about', path: '/about' },
];
const router = createRouter(routes);
function App() {
return (
<RouterProvider router={router}>
// Your application components go here
</RouterProvider>
);
}
export default App;
In this example, we use the RouterProvider component from the react-router v6 library to provide our custom router to the rest of the application. We create a new router using the router6 library, and define our routes using an array of objects. The router is then passed to the RouterProvider component as a prop.
createBrowserRouter: This is a function that allows you to create a custom BrowserRouter instance with your own settings and options. The idea behind createBrowserRouter is that you can fine-tune the behavior of the BrowserRouter to suit your specific needs.
Here's an example of how to use createBrowserRouter to create a custom BrowserRouter instance:
import React from 'react';
import { createBrowserRouter } from 'react-router-dom';
const basename = '/my-app';
const BrowserRouter = createBrowserRouter({
basename: basename,
forceRefresh: false,
});
function App() {
return (
<BrowserRouter>
// Your application components go here
</BrowserRouter>
);
}
export default App;
In this example, we use the createBrowserRouter function to create a custom BrowserRouter instance with a specific basename and forceRefresh setting. The basename option specifies the base URL for all routes, while the forceRefresh option controls whether the router should perform a full page refresh when navigating between routes. The custom BrowserRouter instance is then passed to the App component as a prop.
Overall, RouterProvider and createBrowserRouter can be useful for customizing the behavior of the react-router-dom library to suit your specific needs. However, they are not necessary for most basic use cases, and can add additional complexity to your application.