url ์ฃผ์์ ๋ฐ๋ผ ๋ค๋ฅธ view๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ด๋ค. ๋ฆฌ์กํธ๋ ์ด๋ฐ ๊ธฐ๋ฅ์ด ๋ด์ฅ๋์ด ์์ง ์๊ธฐ ๋๋ฌธ์ ๊ฐ์ฅ ์ธ๊ธฐ์๋ Third-party Library์ธ React-router๋ฅผ ์ฌ์ฉํด๋ณด์.
npm ๋๋ yarn์ผ๋ก ๋ฆฌ์กํธ ๋ผ์ฐํธ ์ค์น ํ index.js์ ๊ฐ์ ์์น์ Routes.js ํ์ผ์ ์์ฑํด์ค๋ค.
//Route.js
import React from 'react';
import {
BrowserRouter as Router,
Switch,
Route,
} from 'react-router-dom';
import Login from './pages/Login/Login';
import List from './pages/List/List';
import Detail from './pages/Detail/Detail';
class Routes extends React.Component {
render() {
return (
<Router> // or <BrouserRouter>
<Switch>
<Route exact path="/" component={Login}/>
<Route exact path="/list" component={List} />
<Route exact path="/detail" component={Detail} />
</Switch>
</Router>
)
}
}
export default Routes;
//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import Routes from './Routes';
import "./styles/reset.scss";
ReactDOM.render(<Routes />, document.getElementById('root'));
<Link>
์ปดํฌ๋ํธ ์ฌ์ฉ<a>
๋ก ๋ณํ๋๋ค. ํ์ง๋ง ์ธ๋ถ ์ฌ์ดํธ๋ก ์ด๋์ํค๋ aํ๊ทธ์ ๋ฌ๋ฆฌ Link๋ ํ๋ก์ ํธ ๋ด์์ ํ์ด์ง๋ฅผ ์ ํ์์ผ์ค๋ค. //Login.js
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import './Login.scss';
class Login extends Component {
render() {
return(
<>
</>
)
}
}
export default Login;
import React from 'react';
import { withRouter } from 'react-router-dom';
class Login extends Component {
goToListPage = () => {
this.props.history.push('/list');
}
render() {
return (
<div>
<button
className="loginBtn"
onClick={this.goToListPage}
>
๋ก๊ทธ์ธ
</button>
</div>
)
}
}
export default withRouter(Login); //์ฃผ์