react router - slahsk/study GitHub Wiki
import React, { Component } from 'react';
import { render } from 'react-dom';
import About from './About';
import Home from './Home';
import Repos from './Repos';
class App extends Component{
constructor(){
super(...arguments);
this.state ={
route : window.location.hash.substr(1)
};
}
componentDidMount(){
window.addEventListener('hashchange', () => {
this.setState({
route:window.location.hash.substr(1)
});
});
}
render(){
var Child;
switch (this.state.route) {
case '/about':Child = About;break;
case '/repos':Child = Repos;break;
default:Child = Home;
}
return(
<div>
<header>App</header>
<menu>
<ul>
<li><a href="#/about">About</a></li>
<li><a href="#/repos">Repos</a></li>
</ul>
</menu>
<Child/>
</div>
);
}
}
render(<App />, document.getElementById('root'));์ปดํฌ๋ํธ๊ฐ ๋ง์ดํธ๋ ๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์ถ๊ฐํด URL ๋ฐ๋ ๋๋ง๋ค route ์ํ๋ฅผ ์ ๋ฐ์ดํธํ๊ณ ์ปดํฌ๋ํธ๋ฅผ ๋ค์ ๋ ๋๋งํ๊ฒ ํ๋ค.
- URL ๊ด๋ฆฌ๊ฐ ํต์ฌ์ ์ธ ์์ ์ด ๋๋ค.
- ์ง์ URL์ ์์ ํ๊ณ ์กฐ์
- ๋ผ์ฐํ ์ฝ๋๊ฐ ๋ณต์กํด์ง ์ ์๋ค.
- ์ค์ฒฉ๋ URL๊ณผ ์ค์ฒฉ๋ ์ปดํฌ๋ํธ ๊ณ์ธต์ ๋ฆฌ์กํธ ๋ผ์ฐํฐ์ ์ ์ต์ API์์ ํต์ฌ์ ์ธ ๊ฐ๋ ์ด๋ฉฐ, ๋ฆฌ์กํธ ์ฝ์ด์ ์ผ๋ถ๋ ์๋์ง๋ง ๋ฆฌ์กํธ ์ปค๋ฎค๋ํฐ์์ ํ์ค์ผ๋ก ์ธ์ ๋ฐ๊ณ ์๋ค.
- ์ปดํฌ๋ํธ๋ฅผ ์ค์ฒฉ ๋จ๊ณ์ ๊ด๊ณ์์ด ๋ผ์ฐํธ์ ์ฐ๊ฒฐํ๋ ๋ฐฉ๋ฒ์ผ๋ก UI๋ฅผ URL๊ณผ ๋๊ธฐํ ํ๋ค.
- URL์ ๋ณ๊ฒฝํ๋ฉด ์๋์ผ๋ก ์ธ๋ง์ดํธ ๋ฐ ๋ง์ดํธ ๋๋ค.
import React, { Component } from 'react';
import { render } from 'react-dom';
import { Router, Route, Link, browserHistory , IndexRoute} from 'react-router';
import './style.css';
import About from './About';
import Home from './Home';
import Repos from './Repos';
import RepoDetails from './RepoDetails';
import ServerError from './ServerError';
class App extends Component{
render(){
return(
<div>
<header>App</header>
<menu>
<ul>
<li><Link to="/about" activeClassName="active">About</Link></li>
<li><Link to="/repos" activeClassName="active">Repos</Link></li>
</ul>
</menu>
{this.props.children}
</div>
);
}
}
render((
<Router history={browserHistory} >
<Route path="/" component={App}>
<IndexRoute component={Home} />
<Route path="about" component={About} title="About Us"/>
<Route path="repos" component={Repos}>
{/* <Route path="details/:repo_name" component={RepoDetails}/> */}
<Route path="/repo/:repo_name" component={RepoDetails}/>
</Route>
<Route path="error" component={ServerError} />
</Route>
</Router>
), document.getElementById('root'));/ ๋ผ์ฐํธ์ฅ์ ์๋ฒ์ ์ ๊ทผํ๋ฉด ์์์ด ์๋ App ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง ๋๋ค.
<IndexRoute component={Home} />
<Route path="/repo/:repo_name" component={RepoDetails}/>
-
:repo_name๋งค๊ฐ๋ณ์๋ฅผ ์ปดํฌ๋ํธ์ ์์ฑ์ ์ฃผ์ ํ๋ค. -
RepoDetails์์componentWillReceiveProps๋ผ๋ ์ถ๊ฐ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋์์๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ผ ํ๋ค. ๋ง์ดํ ๋ ํ์๋ ์ฌ์ฉ์๊ฐ ๋ค๋ฅธ ๋ฆฌํฌ์งํ ๋ฆฌ๋ฅผ ํด๋ฆญํ๋ฉด ์๋ก์ด ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์ ์ ์์ผ๋ฉฐ๋ฉฐ ์ด๊ฒฝ์ฐcomponentDidMount๊ฐ ํธ์ถ๋์ง ์๊ณcomponentWillReceiveProps๊ฐ ํธ์ถ๋๊ธฐ ๋๋ฌธ์ด๋ค.
componentDidMount(){
let repo_name = this.props.params.repo_name;
this.fetchData(repo_name);
}
componentWillReceiveProps(nextProps){
let repo_name = nextProps.params.repo_name;
this.fetchData(repo_name);
} <Route path="about" component={About} title="About Us"/> ์ ๋ฌํ๋ฉด {this.props.route.title} ํตํด ์ ๊ทผํ ์ ์๋ค.
๋์ ์ผ๋ก ์ปดํฌ๋ํธ๋ก ์ ๋ฌํ๊ธฐ ์ํด์๋ {this.props.children} ๊ทธ๋๋ก ๋ ๋๋งํ๋ ๋์ ์ด๋ฅผ ๋ณต์ ํ๊ณ ์ถ๊ฐ ์์์ ์ฃผ์
ํ ์ ์๋ค.
let child = this.props.children && React.cloneElement(this.props.children, {
repositories : this.state.repositories
});https://github.com/ReactTraining/react-router/blob/master/docs/API.md
Link ์ปดํฌ๋ํธ๋ ์ต์ข
์ฌ์ฉ์๊ฐ ๋ผ์ฐํธ์ ์ํธ์์ฉํ๋ ํ๋ฅญํ ๋ฐฉ๋ฒ์ด์ง๋ง ์ข
์ข
์ปดํฌ๋ํธ ์์์
ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ๋ผ์ฐํธ๋ฅผ ์ฒ๋ฆฌํด์ผ ํ ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ์ฆ, ํน์ ํ ์ํฉ์์ ์๋์ผ๋ก ์ด์ ์ผ๋ก ๋์๊ฐ๊ฑฐ๋ ์ฌ์ฉ์๋ ๋ค๋ฅธ ๋ผ์ฐํธ๋ก ๋ฆฌ๋๋ ์
ํด์ผ ํ ์ ์๋ค.
์ด๋ฌํ ๋ชฉ์ ์ผ๋ก ๋ฆฌ์กํธ ๋ผ์ฐํฐ๋ ๋ง์ดํ
ํ๋ ๋ชจ๋ ์ปดํฌ๋ํธ์ ์๋์ผ๋ก ์์ฒด history ๊ฐ์ฒด๋ฅผ ์ฃผ์
ํ๋ค
history ๊ฐ์ฒด๋ ๋ถ๋ผ์ฐ์ ์ ํ์คํ ๋ฆฌ ์คํ์ ๊ด๋ฆฌํ๋ ์ญํ์ ํ๋ค.
| ๋ฉ์๋ | ์ค๋ช |
|---|---|
| pushState | ์๋ก์ด URL๋ก ์ด๋ํ๋ ๊ธฐ๋ณธ ํ์คํ ๋ฆฌ ํ์ ๋ฉ์๋์ด๋ฉฐ ์ต์
๋งค๊ฐ๋ณ์ ๊ฐ์ฒด๋ฅผ ์ง์ ํ ์ ์๋ค. history.pushState(null, '/users/123') history.pushState({showGrades:true},'/users/123') |
| replaceState | pushState์ ๋์ผํ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ๋ฉฐ ํ์ฌ URL์ ์๋ก์ด URL๋ก ๋์ฒดํ๋ค ํ์คํ ๋ฆฌ์ ๊ธธ์ด์ ์ํฅ์ ์ฃผ์ง ์๊ณ URL์ ๋์ฒดํ๋ค๋ ์ ์์ ๋ฆฌ๋๋ ์ ๊ณผ ๋น์ทํ๋ค. |
| goBack | ํ์ ํ์คํ ๋ฆฌ์์ ํ ํญ๋ชฉ ๋ค๋ก ์ด๋ํ๋ค. |
| goForward | ํ์ ํ์คํ ๋ฆฌ์์ ํ ํญ๋ชฉ ์์ผ๋ก ์ด๋ํ๋ค. |
| Go | n ๋๋ -n๋งํผ ์์ผ๋ก ๋๋ ๋ค๋ก ์ด๋ํ๋ค. |
| createHref | ๋ผ์ฐํฐ์ ๊ตฌ์ฑ์ ์ด์ฉํด URL์ ๋ง๋ ๋ค. |
// v1.x
<Router/>
// v2.0.0
// hash history
import { hashHistory } from 'react-router'
<Router history={hashHistory} />
// v1.x
import createBrowserHistory from 'history/lib/createBrowserHistory'
<Router history={createBrowserHistory()} />
// v2.0.0
import { browserHistory } from 'react-router'
<Router history={browserHistory} />
// v1.0.x
history.pushState(state, path, query)
history.replaceState(state, path, query)
// v2.0.0
router.push(path)
router.push({ pathname, query, state }) // new "location descriptor"
router.replace(path)
router.replace({ pathname, query, state }) // new "location descriptor"
https://github.com/ReactTraining/react-router/blob/master/upgrade-guides/v2.0.0.md