-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.jsx
47 lines (38 loc) · 1.45 KB
/
app.jsx
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
import Home from './components/home';
import Time from './components/time';
import Counter from './components/counter';
import './app.css'
const { useState, useEffect } = React;
export default function App() {
const [route, setRoute] = useState(window.location.pathname);
useEffect(() => {
const handlePopState = () => setRoute(window.location.pathname);
window.addEventListener('popstate', handlePopState);
return () => window.removeEventListener('popstate', handlePopState);
}, []);
const navigate = (path) => {
window.history.pushState({}, '', path);
setRoute(path);
};
let Component;
switch (route) {
case '/time':
Component = Time;
break;
case '/counter':
Component = Counter;
break;
default:
Component = Home;
}
return (
<>
<nav style={{ backgroundColor: '#f0f0f0', padding: '10px', textAlign: 'center' }}>
<button onClick={() => navigate('/')} style={{ margin: '0 10px', padding: '5px 10px' }}>Home</button>
<button onClick={() => navigate('/time')} style={{ margin: '0 10px', padding: '5px 10px' }}>Time</button>
<button onClick={() => navigate('/counter')} style={{ margin: '0 10px', padding: '5px 10px' }}>Counter</button>
</nav>
<Component />
</>)
}
ReactDOM.render(<App />, document.getElementById('root'));