31. Routing (React Router)
Creating the Multi-Page Feel
React is technically a Single Page Application (SPA) library. By default, it has no concept of "pages" or "URLs". If a user wants to visit an "About Us" page, traditional HTML would make the browser download a completely new about.html file. In React, we use React Router to intercept the URL and instantly swap out components, creating lightning-fast, seamless navigation.
Setup: The BrowserRouter
To use React Router, you must wrap your entire application inside a <BrowserRouter>. This connects React to the browser's URL history API.
Navigation: The Link Component
Crucial Rule: Never use a standard HTML <a href="/"> tag for internal navigation in React. It will force the browser to do a hard refresh, which destroys all your React state! Always use React Router's <Link> component instead.
Dynamic Routes (URL Parameters)
If you have an e-commerce store, you can't create a hardcoded route for every single product. You create a dynamic route using a colon :. The variable is captured and can be read by the component.
Knowledge Check
Ready to test your understanding of 31. Routing (React Router)?