React Router is a powerful library for handling routing in React applications. Beyond basic route definitions, React Router allows for more advanced routing techniques such as nested routes, dynamic routing, and programmatic navigation. These techniques are essential for building complex, scalable applications with organized and maintainable code.
Key Concepts:
Nested routes are used to render child components within a parent component’s layout. This is useful for creating layouts like dashboards, where multiple sections need to be rendered within the same page.
Example:
import React from 'react';
import { BrowserRouter as Router, Route, Switch, Link, useRouteMatch } from 'react-router-dom';
function App() {
return (
<Router>
<nav>
<Link to="/dashboard">Dashboard</Link>
</nav>
<Switch>
<Route path="/dashboard" component={Dashboard} />
</Switch>
</Router>
);
}
function Dashboard() {
let { path, url } = useRouteMatch(); // Hook to get the current path and url
return (
<div>
<h2>Dashboard</h2>
<nav>
<Link to={`${url}/overview`}>Overview</Link>
<Link to={`${url}/settings`}>Settings</Link>
</nav>
<Switch>
<Route exact path={path}>
<h3>Please select a section.</h3>
</Route>
<Route path={`${path}/overview`} component={Overview} />
<Route path={`${path}/settings`} component={Settings} />
</Switch>
</div>
);
}
function Overview() {
return <h3>Overview Section</h3>;
}
function Settings() {
return <h3>Settings Section</h3>;
}
export default App;
In this example:
/dashboard route renders the Dashboard component.Dashboard, the routes for /dashboard/overview and /dashboard/settings are defined. The useRouteMatch hook helps in constructing these paths dynamically based on the current URL.Dashboard component use the url provided by useRouteMatch to navigate to the nested routes.Dynamic routing allows routes to be created based on dynamic segments, such as user IDs or search queries. This is particularly useful for pages that need to display specific content based on a parameter.
Example:
import React from 'react';
import { BrowserRouter as Router, Route, Switch, useParams } from 'react-router-dom';
function App() {
return (
<Router>
<Switch>
<Route path="/user/:id" component={UserProfile} />
</Switch>
</Router>
);
}
function UserProfile() {
let { id } = useParams(); // Hook to access the dynamic parameter
return (
<div>
<h2>User Profile for ID: {id}</h2>
{/* Fetch and display user data based on the ID */}
</div>
);
}
export default App;
In this example:
/user/:id route is defined with a dynamic segment :id.useParams hook extracts the id parameter from the URL, allowing the UserProfile component to fetch and display data based on the user ID.Programmatic navigation allows you to change the route in response to user actions without requiring the user to click a link. This is useful for scenarios like redirecting after form submission or conditionally navigating based on user input.
Example:
import React from 'react';
import { BrowserRouter as Router, Route, Switch, useHistory } from 'react-router-dom';
function App() {
return (
<Router>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/dashboard" component={Dashboard} />
</Switch>
</Router>
);
}
function Home() {
let history = useHistory(); // Hook to get history object
const handleLogin = () => {
// Perform login logic...
history.push('/dashboard'); // Navigate to dashboard after login
};
return (
<div>
<h2>Home</h2>
<button onClick={handleLogin}>Login</button>
</div>
);
}
function Dashboard() {
return <h2>Dashboard</h2>;
}
export default App;
In this example:
useHistory hook provides access to the history object, which allows you to navigate programmatically./dashboard route using history.push().For this part of the lesson, apply what you’ve learned to your course project:
Example Tasks:
To reinforce your understanding of advanced routing techniques, here are some helpful videos:
For further reading and exploration: