/
14-Routing.js
83 lines (70 loc) · 2.39 KB
/
14-Routing.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
import React, { Component } from 'react'
import ReactDOM from 'react-dom';
import { Router, Route, Link, IndexRoute, hashHistory, browserHistory, DefaultRoute, IndexLink } from 'react-router';
class App extends Component {
render () {
return (
<Router history={hashHistory}>
<Route path='/' component={Container}>
<IndexRoute component={Home} />
<Route path='/address' component={Address}>
<IndexRoute component={TwitterFeed} />
<Route path='instagram' component={Instagram} />
<Route path='query' component={Query} />
</Route>
<Route path='/about(/:name)' component={About} />
<Route path='/namedComponent' component={NamedComponents}>
<IndexRoute components={{ title: Title, subTitle: SubTitle }} />
</Route>
<Route path='*' component={NotFound} />
</Route>
</Router>
)
}
}
const Query = (props) => (
<h2>{props.location.query.message}</h2>
)
const Title = () => (
<h1>Hello from Title Component</h1>
)
const SubTitle = () => (
<h2>Hello from SubTitle Component</h2>
)
const NamedComponents = (props) => (
<div>
{props.title}<br />
{props.subTitle}
</div>
)
const Nav = () => (
<div>
<IndexLink activeClassName='active' to='/'>Home</IndexLink>
<IndexLink activeClassName='active' to='/address'>Address</IndexLink>
<IndexLink activeClassName='active' to='/about'>About</IndexLink>
<IndexLink activeClassName='active' to='/namedComponent'>Named Components</IndexLink>
<IndexLink activeClassName='active' to={{ pathname: '/address/query', query: { message: 'Hello from Route Query' } }}>Route Query</IndexLink>
</div>
)
const Container = (props) => <div>
<Nav />
{props.children}
</div>
const Home = () => <h1>Hello from Home!</h1>
const Address = (props) => <div>
<br />
<Link to='/address'>Twitter Feed</Link>
<Link to='/address/instagram'>Instagram Feed</Link>
<h1>We are located at 555 Jackson St.</h1>
{props.children}
</div>
const Instagram = () => <h3>Instagram Feed</h3>
const TwitterFeed = () => <h3>Twitter Feed</h3>
const About = (props) => (
<div>
<h3>Welcome to the About Page</h3>
{ props.params.name && <h2>Hello, {props.params.name}</h2>}
</div>
)
const NotFound = () => <h1>404.. This page is not found!</h1>
ReactDOM.render(<App />, document.getElementById('app'));