Skip to content

Instantly share code, notes, and snippets.

@alan-ps
Created August 15, 2019 08:06
Show Gist options
  • Save alan-ps/d53a383d109578bca44ea705a5e4e475 to your computer and use it in GitHub Desktop.
Save alan-ps/d53a383d109578bca44ea705a5e4e475 to your computer and use it in GitHub Desktop.
An example of simple routing implementation for your React application.
import React, {Component} from 'react';
import { Redirect, BrowserRouter, Route, Switch } from "react-router-dom";
function App() {
let auth = false;
return (
<BrowserRouter>
<Switch>
<Route
exact
path="/"
render={() => (auth ? (<Redirect to="/dashboard" />) : (<Front />))}
/>
<Route
path="/dashboard"
render={() => (!auth ? (<Redirect to="/sign-in" />) : (<Dashboard />))}
/>
<Route
path="/sign-in"
render={() => (auth ? (<Redirect to="/dashboard" />) : (<SignIn />))}
/>
<Route
path="/sign-up"
render={() => (auth ? (<Redirect to="/dashboard" />) : (<SignUp />))}
/>
<Route
component={NoMatch}
/>
</Switch>
</BrowserRouter>
);
}
function Front() {
return <h1>Front</h1>;
}
function Dashboard() {
return <h1>Dashboard</h1>;
}
function SignIn() {
return <h1>SignIn</h1>;
}
function SignUp() {
return <h1>SignUn</h1>;
}
function NoMatch() {
return <h1>404</h1>;
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment