@@ -48,22 +48,58 @@ As the hash portion of the URL changes, `App` will render a different
48
48
` <Child/> ` by branching on ` this.state.route ` . Pretty straightforward
49
49
stuff. But it gets complicated fast.
50
50
51
- Imagine now that ` Inbox ` has some nested UI at a path like
52
- ` inbox/messages/:id ` and ` inbox/unread ` , etc. We'll need to make our url
53
- parsing much more intelligent to be able to pass the right information
54
- to ` App ` , and then to ` Inbox ` in order for it to know which URL-mapped
55
- child component it should render. We'd then have a branch of components
56
- that should be rendered at any given URL. Add a few more of these
57
- branches and we'll end up with a lot of code to keep the URL and our
58
- application's component hierarchy in sync.
51
+ Imagine now that ` Inbox ` has some nested UI at different URLs, maybe
52
+ something like this master detail view:
53
+
54
+ ```
55
+ path: /inbox/messages/1234
56
+
57
+ +---------+------------+------------------------+
58
+ | About | Inbox | |
59
+ +---------+ +------------------------+
60
+ | Compose Reply Reply All Archive |
61
+ +-----------------------------------------------+
62
+ |Movie tomorrow| |
63
+ +--------------+ Subject: TPS Report |
64
+ |TPS Report From: [email protected] |
65
+ +--------------+ |
66
+ |New Pull Reque| So ... |
67
+ +--------------+ |
68
+ |... | |
69
+ +--------------+--------------------------------+
70
+ ```
71
+
72
+ And maybe stats page when not viewing a message:
73
+
74
+ ```
75
+ path: /inbox
76
+
77
+ +---------+------------+------------------------+
78
+ | About | Inbox | |
79
+ +---------+ +------------------------+
80
+ | Compose Reply Reply All Archive |
81
+ +-----------------------------------------------+
82
+ |Movie tomorrow| |
83
+ +--------------+ 10 Unread Messages |
84
+ |TPS Report | 22 drafts |
85
+ +--------------+ |
86
+ |New Pull Reque| |
87
+ +--------------+ |
88
+ |... | |
89
+ +--------------+--------------------------------+
90
+ ```
91
+
92
+ We'd have to make our url parsing a lot more intelligent, and end up
93
+ with a lot of code to figure out which branch of nested components to be
94
+ rendered at any given url: ` App -> About ` , `App -> Inbox -> Messages ->
95
+ Message` , ` App -> Inbox -> Messages -> Stats`, etc.
59
96
60
97
With React Router
61
98
-----------------
62
99
63
100
Nested URLs and nested component hierarchy are at the heart of React
64
- Router. It brings a declarative API to your routes. Lots of people like
65
- to use JSX to define their routes, but you can use plain objects if you
66
- want.
101
+ Router's declarative API. Lots of people like to use JSX to define their
102
+ routes, but you can use plain objects if you want.
67
103
68
104
Let's refactor our app to use React Router.
69
105
0 commit comments