10-router.html
html9 kB199 linesIn Tutorial.zip
Tutorial.zip10-router.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React Router - React Tutorial</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<header>
<h1>⚛️ React Router</h1>
<p>Chapter 10 of 15</p>
</header>
<div class="content">
<h2>What is React Router?</h2>
<p>React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL.</p>
<div class="info-box">
<strong>💡 Why React Router?</strong> Single-page applications need a way to handle multiple "pages" without refreshing. React Router makes this possible!
</div>
<h2>Installation</h2>
<p>To use React Router, first install the package:</p>
<pre><code>npm install react-router-dom</code></pre>
<h2>Basic Routing</h2>
<p>Here's a complete example of basic routing:</p>
<pre><code><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">"react"</span>;
<span class="keyword">import</span> { BrowserRouter, Routes, Route } <span class="keyword">from</span> <span class="string">"react-router-dom"</span>;
<span class="keyword">import</span> Layout <span class="keyword">from</span> <span class="string">"./pages/Layout"</span>;
<span class="keyword">import</span> Home <span class="keyword">from</span> <span class="string">"./pages/Home"</span>;
<span class="keyword">import</span> About <span class="keyword">from</span> <span class="string">"./pages/About"</span>;
<span class="keyword">import</span> Contact <span class="keyword">from</span> <span class="string">"./pages/Contact"</span>;
<span class="keyword">import</span> NoPage <span class="keyword">from</span> <span class="string">"./pages/NoPage"</span>;
<span class="keyword">export default function</span> <span class="function">App</span>() {
<span class="keyword">return</span> (
<BrowserRouter>
<Routes>
<Route path=<span class="string">"/"</span> element={<Layout />}>
<Route index element={<Home />} />
<Route path=<span class="string">"about"</span> element={<About />} />
<Route path=<span class="string">"contact"</span> element={<Contact />} />
<Route path=<span class="string">"*"</span> element={<NoPage />} />
</Route>
</Routes>
</BrowserRouter>
);
}</code></pre>
<h2>Key Components</h2>
<h3>BrowserRouter</h3>
<p>Wraps your entire application and enables routing:</p>
<pre><code><BrowserRouter>
<App />
</BrowserRouter></code></pre>
<h3>Routes and Route</h3>
<p>Define your application's routes:</p>
<pre><code><Routes>
<Route path=<span class="string">"/"</span> element={<Home />} />
<Route path=<span class="string">"/about"</span> element={<About />} />
</Routes></code></pre>
<h3>Link</h3>
<p>Create navigation links without page refresh:</p>
<pre><code><span class="keyword">import</span> { Link } <span class="keyword">from</span> <span class="string">"react-router-dom"</span>;
<Link to=<span class="string">"/"</span>>Home</Link>
<Link to=<span class="string">"/about"</span>>About</Link></code></pre>
<h2>Layout Component</h2>
<p>The Layout component includes navigation and uses Outlet to render child routes:</p>
<pre><code><span class="keyword">import</span> { Outlet, Link } <span class="keyword">from</span> <span class="string">"react-router-dom"</span>;
<span class="keyword">const</span> <span class="function">Layout</span> = () => {
<span class="keyword">return</span> (
<>
<nav>
<ul>
<li><Link to=<span class="string">"/"</span>>Home</Link></li>
<li><Link to=<span class="string">"/about"</span>>About</Link></li>
<li><Link to=<span class="string">"/contact"</span>>Contact</Link></li>
</ul>
</nav>
<Outlet />
<>
);
};
<span class="keyword">export default</span> Layout;</code></pre>
<h2>Page Components</h2>
<h3>Home Page</h3>
<pre><code><span class="keyword">const</span> <span class="function">Home</span> = () => {
<span class="keyword">return</span> <h1>Home Page</h1>;
};
<span class="keyword">export default</span> Home;</code></pre>
<h3>About Page</h3>
<pre><code><span class="keyword">const</span> <span class="function">About</span> = () => {
<span class="keyword">return</span> <h1>About Us</h1>;
};
<span class="keyword">export default</span> About;</code></pre>
<h3>404 Page</h3>
<pre><code><span class="keyword">const</span> <span class="function">NoPage</span> = () => {
<span class="keyword">return</span> <h1>404 - Page Not Found</h1>;
};
<span class="keyword">export default</span> NoPage;</code></pre>
<h2>URL Parameters</h2>
<p>You can pass parameters in the URL:</p>
<pre><code><span class="comment">// Define route with parameter</span>
<Route path=<span class="string">"/user/:id"</span> element={<User />} />
<span class="comment">// Access parameter in component</span>
<span class="keyword">import</span> { useParams } <span class="keyword">from</span> <span class="string">"react-router-dom"</span>;
<span class="keyword">function</span> <span class="function">User</span>() {
<span class="keyword">const</span> { id } = <span class="function">useParams</span>();
<span class="keyword">return</span> <h1>User ID: {id}</h1>;
}</code></pre>
<h2>Programmatic Navigation</h2>
<p>Navigate programmatically using the useNavigate hook:</p>
<pre><code><span class="keyword">import</span> { useNavigate } <span class="keyword">from</span> <span class="string">"react-router-dom"</span>;
<span class="keyword">function</span> <span class="function">MyComponent</span>() {
<span class="keyword">const</span> navigate = <span class="function">useNavigate</span>();
<span class="keyword">const</span> <span class="function">goToAbout</span> = () => {
<span class="function">navigate</span>(<span class="string">"/about"</span>);
};
<span class="keyword">return</span> <button onClick={goToAbout}>Go to About</button>;
}</code></pre>
<h2>Nested Routes</h2>
<p>You can nest routes for complex layouts:</p>
<pre><code><Route path=<span class="string">"/"</span> element={<Layout />}>
<Route index element={<Home />} />
<Route path=<span class="string">"products"</span> element={<Products />}>
<Route path=<span class="string">":id"</span> element={<ProductDetail />} />
<Route path=<span class="string">"new"</span> element={<NewProduct />} />
</Route>
</Route></code></pre>
<h2>Active Links</h2>
<p>Use NavLink for links that show active state:</p>
<pre><code><span class="keyword">import</span> { NavLink } <span class="keyword">from</span> <span class="string">"react-router-dom"</span>;
<NavLink
to=<span class="string">"/about"</span>
className={({ isActive }) => isActive ? <span class="string">"active"</span> : <span class="string">""</span>}
>
About
</NavLink></code></pre>
<div class="success-box">
<strong>✨ Best Practices:</strong>
<ul>
<li>Use BrowserRouter at the root of your app</li>
<li>Define routes in a central location</li>
<li>Use Link/NavLink instead of <a> tags</li>
<li>Implement a 404 page with path="*"</li>
<li>Use nested routes for related pages</li>
</ul>
</div>
<h2>What's Next?</h2>
<p>Now that you understand routing, you're ready to learn about different ways to style your React components!</p>
</div>
<div class="navigation">
<a href="09-forms.html" class="nav-button">← Previous: Forms</a>
<a href="index.html" class="home-button">🏠 Home</a>
<a href="11-styling.html" class="nav-button">Next: Styling →</a>
</div>
</div>
</body>
</html>
In Tutorial.zip
01-introduction.html6 kB02-getting-started.html9 kB03-jsx.html10 kB04-components.html10 kB05-props.html11 kB06-state.html14 kB07-hooks.html15 kB08-events.html15 kB09-forms.html17 kB10-router.html9 kB11-styling.html11 kB12-useeffect.html11 kB13-usecontext.html13 kB14-useref.html13 kB15-custom-hooks.html19 kBindex.html5 kBstyles.css4 kB