Skip to content

Instantly share code, notes, and snippets.

@yoshiko-pg
Last active April 28, 2020 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshiko-pg/e7009b8a99c0f428139ea39db5b1b4f5 to your computer and use it in GitHub Desktop.
Save yoshiko-pg/e7009b8a99c0f428139ea39db5b1b4f5 to your computer and use it in GitHub Desktop.
react-router v4でのonEnter代替
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