Last active
April 28, 2020 12:38
-
-
Save yoshiko-pg/e7009b8a99c0f428139ea39db5b1b4f5 to your computer and use it in GitHub Desktop.
react-router v4でのonEnter代替
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 from 'react'; | |
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom'; | |
import App from 'components/app'; | |
import Signup from 'components/signup'; | |
import Signin from 'components/signin'; | |
import Signout from 'components/signout'; | |
const userOnly = (Component) => ({ history, match }) => | |
isAuthenticated() ? <Component history={history} match={match}/> : <Redirect to="/signin"/>; | |
const guestOnly = (Component) => ({ history, match }) => | |
isAuthenticated() ? <Redirect to="/"/> : <Component history={history} match={match}/>; | |
export default () => { | |
return ( | |
<Router> | |
<Switch> | |
<Route exact path="/signup" render={guestOnly(Signup)}/> | |
<Route exact path="/signin" render={guestOnly(Signin)}/> | |
<Route exact path="/signout" component={Signout}/> | |
<Route path="/" render={userOnly(App)}/> | |
</Switch> | |
</Router> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment