Last active
October 30, 2019 19:50
-
-
Save GergKllai1/65ac9af79b5c854dde963b0fce2fe9e1 to your computer and use it in GitHub Desktop.
Restricting routes with ternary operators
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 { Switch, Route } from "react-router-dom"; | |
import { connect } from "react-redux"; | |
import MainIndex from "./MainIndex"; | |
import LoginPage from "./LoginPage"; | |
import RestrictedPage from "./RestrictedPage"; | |
import OnlyTeacher from "./OnlyTeacher"; | |
import OnlyStudent from "./OnlyStudent"; | |
export const Content = ({ user }) => { | |
return ( | |
<> | |
<Switch> | |
<Route exact path="/index" component={MainIndex} /> | |
{user.isSignedIn && ( | |
<Route exact path="/restricted" component={RestrictedPage} /> | |
)} | |
{user.role === "teacher" && ( | |
<Route exact path="/only-teacher" component={OnlyTeacher} /> | |
)} | |
{user.role === "student" && ( | |
<Route exact path="/only-student" component={OnlyStudent} /> | |
)} | |
<Route exact path="/login" component={LoginPage} /> | |
</Switch> | |
</> | |
); | |
}; | |
const mapStateToProps = state => ({ | |
user: state.userReducer | |
}); | |
export default connect( | |
mapStateToProps, | |
null | |
)(Content); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment