UNPKG

2.41 kBJavaScriptView Raw
1import React from 'react';
2import ReactDOM from 'react-dom';
3
4import { AppContainer } from 'react-hot-loader';
5// import walkMD from './walkmd';
6// AppContainer is a necessary wrapper component for HMR
7import {
8 BrowserRouter as Router,
9 Route,
10 HashRouter,
11 Link
12} from 'react-router-dom';
13
14import App from './App';
15
16const Home = () => (
17 <div>
18 <h2>Home</h2>
19 </div>
20)
21
22const About = () => (
23 <div>
24 <h2>About</h2>
25 </div>
26)
27
28const Topic = ({ match }) => (
29 <div>
30 <h3>{match.params.topicId}</h3>
31 </div>
32)
33
34const Topics = ({ match }) => (
35 <div>
36 <h2>Topics</h2>
37 <ul>
38 <li>
39 <Link to={`${match.url}/rendering`}>
40 Rendering with React
41 </Link>
42 </li>
43 <li>
44 <Link to={`${match.url}/components`}>
45 Components
46 </Link>
47 </li>
48 <li>
49 <Link to={`${match.url}/props-v-state`}>
50 Props v. State
51 </Link>
52 </li>
53 </ul>
54
55 <Route path={`${match.url}/:topicId`} component={Topic}/>
56 <Route exact path={match.url} render={() => (
57 <h3>Please select a topic.</h3>
58 )}/>
59 </div>
60)
61
62const BasicExample = () => (
63 <Router>
64 <div>
65 <ul>
66 <li><Link to="/">Home</Link></li>
67 <li><Link to="/about">About</Link></li>
68 <li><Link to="/topics">Topics</Link></li>
69 </ul>
70
71 <hr/>
72 <App />
73 <Route exact path="/" component={Home}/>
74 <Route path="/about" component={About}/>
75 <Route path="/topics" component={Topics}/>
76 </div>
77 </Router>
78)
79
80const render2 = (comp) => {
81 ReactDOM.render(<BasicExample/>, document.getElementById('root'));
82}
83
84const render3 = (Component) => {
85 ReactDOM.render(
86 <AppContainer>
87 <Component/>
88 </AppContainer>,
89 document.getElementById('root')
90 );
91}
92
93const render = (Component) => {
94 ReactDOM.render(
95 <HashRouter>
96
97 <div>
98 <ul>
99 <li><Link to="/">Home</Link></li>
100 <li><Link to="/about">About</Link></li>
101 <li><Link to="/topics">Topics</Link></li>
102 </ul>
103 <AppContainer>
104 <Component/>
105 </AppContainer>
106 <Route path="/about" component={About}/>
107 <Route path="/topics" component={Topics}/>
108 </div>
109 </HashRouter>
110 ,
111 document.getElementById('root')
112 );
113};
114
115
116
117
118render(App);
119
120
121// Hot Module Replacement API
122if (module.hot) {
123 module.hot.accept('./App', () => {
124 render(App)
125 });
126}
\No newline at end of file