Created
November 27, 2019 08:19
-
-
Save u88803494/ca52af81f9d8c1b309be40c9d8294b4c to your computer and use it in GitHub Desktop.
只含改變的部分
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import './App.css'; | |
import Nav from './nav'; | |
import About from './about'; | |
import Home from './home'; | |
import Post from './post'; | |
const removeHashTag = str => { | |
return str.slice(1); | |
} | |
const Route = ({ current, path, Comp }) => (current !== path ? null : <Comp />) | |
class App extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
tab: removeHashTag(window.location.hash) || 'home', | |
} | |
} | |
componentDidMount() { | |
window.addEventListener("hashchange", this.handleHashChange); | |
} // 在載入的時候新增監聽器 | |
componentWillUnmount() { | |
window.removeEventListener(this.handleHashChange); | |
} // 結束之後要拿掉這個 listener | |
handleHashChange = () => { | |
this.setState({ | |
tab: removeHashTag(window.location.hash), | |
}) | |
} | |
render() { | |
const { tab } = this.state | |
return ( | |
<div className="App" > | |
<Nav activeTab={tab} /> | |
<div className="page"> | |
<Route current={tab} path={'home'} Comp={Home} /> | |
<Route current={tab} path={'post'} Comp={Post} /> | |
<Route current={tab} path={'about'} Comp={About} /> | |
</div> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment