|
| 1 | +# Primary Components |
| 2 | + |
| 3 | +There are three primary categories of components in React Router: |
| 4 | + |
| 5 | +- routers, like `<BrowserRouter>` and `<HashRouter>` |
| 6 | +- route matchers, like `<Route>` and `<Switch>` |
| 7 | +- and navigation, like `<Link>`, `<NavLink>`, and `<Redirect>` |
| 8 | + |
| 9 | +We also like to think of the navigation components as "route changers". |
| 10 | + |
| 11 | +All of the components that you use in a web application should be imported from `react-router-dom`. |
| 12 | + |
| 13 | +```js |
| 14 | +import { BrowserRouter, Route, Link } from "react-router-dom"; |
| 15 | +``` |
| 16 | + |
| 17 | +## Routers |
| 18 | + |
| 19 | +At the core of every React Router application should be a router component. For web projects, `react-router-dom` provides `<BrowserRouter>` and `<HashRouter>` routers. The main difference between the two is the way they store the URL and communicate with your web server. |
| 20 | + |
| 21 | +- A `<BrowserRouter>` uses regular URL paths. These are generally the best-looking URLs, but they require your server to be configured correctly. Specifically, your web server needs to serve the same page at all URLs that are managed client-side by React Router. Create React App supports this out of the box in development, and [comes with instructions](https://create-react-app.dev/docs/deployment#serving-apps-with-client-side-routing) on how to configure your production server as well. |
| 22 | +- A `<HashRouter>` stores the current location in [the `hash` portion of the URL](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/hash), so the URL looks something like `http://example.com/#/your/page`. Since the hash is never sent to the server, this means that no special server configuration is needed. |
| 23 | + |
| 24 | +To use a router, just make sure it is rendered at the root of your element hierarchy. Typically you'll wrap your top-level `<App>` element in a router, like this: |
| 25 | + |
| 26 | +```jsx |
| 27 | +import React from "react"; |
| 28 | +import ReactDOM from "react-dom"; |
| 29 | +import { BrowserRouter } from "react-router-dom"; |
| 30 | + |
| 31 | +function App() { |
| 32 | + return <h1>Hello React Router</h1>; |
| 33 | +} |
| 34 | + |
| 35 | +ReactDOM.render( |
| 36 | + <BrowserRouter> |
| 37 | + <App /> |
| 38 | + </BrowserRouter>, |
| 39 | + document.getElementById("root") |
| 40 | +); |
| 41 | +``` |
| 42 | + |
| 43 | +## Route Matchers |
| 44 | + |
| 45 | +There are two route matching components: `Switch` and `Route`. When a `<Switch>` is rendered, it searches through its `children` `<Route>` elements to find one whose `path` matches the current URL. When it finds one, it renders that `<Route>` and ignores all others. This means that you should put `<Route>`s with more specific (typically longer) `path`s **before** less-specific ones. |
| 46 | + |
| 47 | +If no `<Route>` matches, the `<Switch>` renders nothing (`null`). |
| 48 | + |
| 49 | +```jsx |
| 50 | +import React from "react"; |
| 51 | +import ReactDOM from "react-dom"; |
| 52 | +import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; |
| 53 | + |
| 54 | +function App() { |
| 55 | + return ( |
| 56 | + <div> |
| 57 | + <Switch> |
| 58 | + {/* If the current URL is /about, this route is rendered |
| 59 | + while the rest are ignored */} |
| 60 | + <Route path="/about"> |
| 61 | + <About /> |
| 62 | + </Route> |
| 63 | + |
| 64 | + {/* Note how these two routes are ordered. The more specific |
| 65 | + path="/contact/:id" comes before path="/contact" so that |
| 66 | + route will render when viewing an individual contact */} |
| 67 | + <Route path="/contact/:id"> |
| 68 | + <Contact /> |
| 69 | + </Route> |
| 70 | + <Route path="/contact"> |
| 71 | + <AllContacts /> |
| 72 | + </Route> |
| 73 | + |
| 74 | + {/* If none of the previous routes render anything, |
| 75 | + this route acts as a fallback. |
| 76 | +
|
| 77 | + Important: A route with path="/" will *always* match |
| 78 | + the URL because all URLs begin with a /. So that's |
| 79 | + why we put this one last of all */} |
| 80 | + <Route path="/"> |
| 81 | + <Home /> |
| 82 | + </Route> |
| 83 | + </Switch> |
| 84 | + </div> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +ReactDOM.render( |
| 89 | + <Router> |
| 90 | + <App /> |
| 91 | + </Router>, |
| 92 | + document.getElementById("root") |
| 93 | +); |
| 94 | +``` |
| 95 | + |
| 96 | +One important thing to note is that a `<Route path>` matches the **beginning** of the URL, not the whole thing. So a `<Route path="/">` will **always** match the URL. Because of this, we typically put this `<Route>` last in our `<Switch>`. Another possible solution is to use `<Route exact path="/">` which **does** match the entire URL. |
| 97 | + |
| 98 | +Note: Although React Router does support rendering `<Route>` elements outside of a `<Switch>`, as of version 5.1 we recommend you use [the `useRouteMatch` hook](#TODO) instead. Additionally, we do not recommend you render a `<Route>` without a `path` and instead suggest you use [a hook](#TODO) to get access to whatever variables you need. |
| 99 | + |
| 100 | +## Navigation (or Route Changers) |
| 101 | + |
| 102 | +React Router provides a `<Link>` component to create links in your application. Wherever you render a `<Link>`, an anchor (`<a>`) will be rendered in your HTML document. |
| 103 | + |
| 104 | +```jsx |
| 105 | +<Link to="/">Home</Link> |
| 106 | +// <a href="/">Home</a> |
| 107 | +``` |
| 108 | + |
| 109 | +The `<NavLink>` is a special type of `<Link>` that can style itself as "active" when its `to` prop matches the current location. |
| 110 | + |
| 111 | +```jsx |
| 112 | +<NavLink to="/react" activeClassName="hurray"> |
| 113 | + React |
| 114 | +</NavLink> |
| 115 | + |
| 116 | +// When the URL is /react, this renders: |
| 117 | +// <a href="/react" className="hurray">React</a> |
| 118 | + |
| 119 | +// When it's something else: |
| 120 | +// <a href="/react">React</a> |
| 121 | +``` |
| 122 | + |
| 123 | +Any time that you want to force navigation, you can render a `<Redirect>`. When a `<Redirect>` renders, it will navigate using its `to` prop. |
| 124 | + |
| 125 | +```jsx |
| 126 | +<Redirect to="/login" /> |
| 127 | +``` |
0 commit comments