forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (82 loc) · 2.05 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React from 'react'
import { render } from 'react-dom'
import { browserHistory, Router, Route, Link } from 'react-router'
import withExampleBasename from '../withExampleBasename'
import data from './data'
import './app.css'
const Category = ({ children, params }) => {
const category = data.lookupCategory(params.category)
return (
<div>
<h1>{category.name}</h1>
{children || (
<p>{category.description}</p>
)}
</div>
)
}
const CategorySidebar = ({ params }) => {
const category = data.lookupCategory(params.category)
return (
<div>
<Link to="/">◀︎ Back</Link>
<h2>{category.name} Items</h2>
<ul>
{category.items.map((item, index) => (
<li key={index}>
<Link to={`/category/${category.name}/${item.name}`}>{item.name}</Link>
</li>
))}
</ul>
</div>
)
}
const Item = ({ params: { category, item } }) => {
const menuItem = data.lookupItem(category, item)
return (
<div>
<h1>{menuItem.name}</h1>
<p>${menuItem.price}</p>
</div>
)
}
const Index = () => (
<div>
<h1>Sidebar</h1>
<p>
Routes can have multiple components, so that all portions of your UI
can participate in the routing.
</p>
</div>
)
const IndexSidebar = () => (
<div>
<h2>Categories</h2>
<ul>
{data.getAll().map((category, index) => (
<li key={index}>
<Link to={`/category/${category.name}`}>{category.name}</Link>
</li>
))}
</ul>
</div>
)
const App = ({ content, sidebar }) => (
<div>
<div className="Sidebar">
{sidebar || <IndexSidebar />}
</div>
<div className="Content">
{content || <Index />}
</div>
</div>
)
render((
<Router history={withExampleBasename(browserHistory, __dirname)}>
<Route path="/" component={App}>
<Route path="category/:category" components={{ content: Category, sidebar: CategorySidebar }}>
<Route path=":item" component={Item} />
</Route>
</Route>
</Router>
), document.getElementById('example'))