ReactJS - Redirect
Redirect I - Simple
~cra-router/src/routes$ Login.js
import React from 'react';
const Login = () => {
return (
<div>
로그인
</div>
);
};
export default Login;
~cra-router/src/routes$ MyPage.js
import React from 'react';
import { Redirect } from 'react-router-dom';
const logged = true;
const MyPage = () => {
return (
<div>
{
!logged && <Redirect to="/login"/>
}
마이페이지
</div>
);
};
export default MyPage;
~cra-router/src$ App.js
import React from 'react';
import { BrowserRouter as Router, Route} from 'react-router-dom';
import Home from './routes/Home';
import About from './routes/About';
import Posts from './routes/Posts';
import Login from './routes/Login';
import MyPage from './routes/MyPage';
import Header from './components/Header';
const App = () => {
return (
<Router>
<div>
<Header />
<Route exact path='/' component={Home} />
<Route path='/about/:username' component={About} />
<Route path='/posts' component={Posts} />
<Route path='/login' component={Login} />
<Route path='/mypage' component={MyPage} />
</div>
</Router>
)
}
export default App;
Redirect II - Using History
'my_lesson > _ReactJS' 카테고리의 다른 글
React-router - NotFound (Switch) (0) | 2018.04.11 |
---|---|
React-router - (URLSearchParams(location.search).get('keyword') (0) | 2018.04.11 |
React-router - NavLink (0) | 2018.04.11 |
React-router - Add Route in route (double route) (0) | 2018.04.11 |
React-router - Add URL Parameter (0) | 2018.04.11 |
댓글