|
| 1 | +# Nested Routes |
| 2 | + |
| 3 | +The navigation we added to `App` should probably be present on every |
| 4 | +screen. Without React Router, we could wrap that `ul` into a |
| 5 | +component, say `Nav`, and render a `Nav` on every one of our screens. |
| 6 | + |
| 7 | +This approach isn't as clean as the application grows. React Router |
| 8 | +provides another way to share UI like this with nested routes, a trick |
| 9 | +it learned from [Ember](http://emberjs.com) (/me tips hat). |
| 10 | + |
| 11 | +## Nested UI and Nested URLs |
| 12 | + |
| 13 | +Have you ever noticed your app is just a series of boxes inside boxes |
| 14 | +inside boxes? Have you also noticed your URLs tend to be coupled to that |
| 15 | +nesting? For example given this url, `/repos/123`, our |
| 16 | +components would probably look like this: |
| 17 | + |
| 18 | +```js |
| 19 | +<App> {/* / */} |
| 20 | + <Repos> {/* /repos */} |
| 21 | + <Repo/> {/* /repos/123 */} |
| 22 | + </Repos> |
| 23 | +</App> |
| 24 | +``` |
| 25 | + |
| 26 | +And our UI something like: |
| 27 | + |
| 28 | +``` |
| 29 | + +-------------------------------------+ |
| 30 | + | Home Repos About | <- App |
| 31 | + +------+------------------------------+ |
| 32 | + | | | |
| 33 | +Repos -> | repo | Repo 1 | |
| 34 | + | | | |
| 35 | + | repo | Boxes inside boxes | |
| 36 | + | | inside boxes ... | <- Repo |
| 37 | + | repo | | |
| 38 | + | | | |
| 39 | + | repo | | |
| 40 | + | | | |
| 41 | + +------+------------------------------+ |
| 42 | +``` |
| 43 | + |
| 44 | +React Router embraces this by letting you nest your routes, which |
| 45 | +automatically becomes nested UI. |
| 46 | + |
| 47 | +## Sharing Our Navigation |
| 48 | + |
| 49 | +Lets nest our `About` and `Repos` components inside of `App` so that we |
| 50 | +can share the navigation with all screens in the app. We do it in two |
| 51 | +steps: |
| 52 | + |
| 53 | +First, let the `App` `Route` have children, and move the other routes |
| 54 | +underneath it. |
| 55 | + |
| 56 | +```js |
| 57 | +// index.js |
| 58 | +// ... |
| 59 | +render(( |
| 60 | + <Router history={hashHistory}> |
| 61 | + <Route path="/" component={App}> |
| 62 | + {/* make them children of `App` */} |
| 63 | + <Route path="/repos" component={Repos}/> |
| 64 | + <Route path="/about" component={About}/> |
| 65 | + </Route> |
| 66 | + </Router> |
| 67 | +), document.getElementById('app')) |
| 68 | +``` |
| 69 | + |
| 70 | +Next, render children inside of `App`. |
| 71 | + |
| 72 | +```js |
| 73 | +// modules/App.js |
| 74 | +// ... |
| 75 | + render() { |
| 76 | + return ( |
| 77 | + <div> |
| 78 | + <h1>Ghettohub Issues</h1> |
| 79 | + <ul role="nav"> |
| 80 | + <li><Link to="/about">About</Link></li> |
| 81 | + <li><Link to="/repos">Repos</Link></li> |
| 82 | + </ul> |
| 83 | + |
| 84 | + {/* add this */} |
| 85 | + {this.props.children} |
| 86 | + |
| 87 | + </div> |
| 88 | + ) |
| 89 | + } |
| 90 | +// ... |
| 91 | +``` |
| 92 | + |
| 93 | +Alright, now go click the links and notice that the `App` component |
| 94 | +continues to render while the child route's component gets swapped |
| 95 | +around as `this.props.children` :) |
| 96 | + |
| 97 | +React Router is constructing your UI like this: |
| 98 | + |
| 99 | +```js |
| 100 | +// at /about |
| 101 | +<App> |
| 102 | + <About/> |
| 103 | +</App> |
| 104 | + |
| 105 | +// at /repos |
| 106 | +<App> |
| 107 | + <Repos/> |
| 108 | +</App> |
| 109 | +``` |
| 110 | + |
| 111 | +## By Small and Simple Things are Great Things Brought to Pass |
| 112 | + |
| 113 | +The best way to build large things is to stitch small things together. |
| 114 | + |
| 115 | +This is the real power of React Router, every route can be developed |
| 116 | +(even rendered!) as an independent application. Your route configuration |
| 117 | +stitches all these apps together however you'd like. Applications |
| 118 | +inside of Applications, boxes inside of boxes. |
| 119 | + |
| 120 | +What happens if you move the `About` route outside of `App`? |
| 121 | + |
| 122 | +Okay, now put it back. |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +[Next: Active Links](05-active-links.md) |
0 commit comments