├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.js ├── index.css ├── index.js ├── pages │ ├── DashboardPage │ │ ├── DashboardView.js │ │ ├── SettingsView.js │ │ ├── StatisticsView.js │ │ └── index.js │ ├── LoginPage.js │ └── NotFoundPage.js ├── routes.js ├── serviceWorker.js └── shared │ ├── assets │ └── images │ │ └── mountain_background.png │ ├── layout │ ├── AuthLayout.js │ └── PublicLayout.js │ └── routes │ └── RouteWithProps.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Welcome 2 | 3 | A React web app with a mature react-router setup. Clone this for a head-start for any project! 4 | 5 | Step by step tutorial on [Medium](https://medium.com/javascript-in-plain-english/the-only-react-router-set-up-you-will-ever-need-9f36ddee03a5) 6 | 7 | ![App in action](https://media.giphy.com/media/QWwGHPHlF3Xs6V8xf4/giphy.gif) 8 | 9 | ## Setup 10 | 11 | None -- You are good to go! 12 | 13 | ## Starting the app 14 | 15 | ### `npm run start` 16 | 17 | Runs the app in the development mode.
18 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-router-layout-guide", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "antd": "^3.25.2", 7 | "history": "^4.10.1", 8 | "react": "^16.12.0", 9 | "react-dom": "^16.12.0", 10 | "react-router-dom": "^5.1.2", 11 | "react-scripts": "3.2.0" 12 | }, 13 | "scripts": { 14 | "start": "react-scripts start", 15 | "build": "react-scripts build", 16 | "test": "react-scripts test", 17 | "eject": "react-scripts eject" 18 | }, 19 | "eslintConfig": { 20 | "extends": "react-app" 21 | }, 22 | "browserslist": { 23 | "production": [ 24 | ">0.2%", 25 | "not dead", 26 | "not op_mini all" 27 | ], 28 | "development": [ 29 | "last 1 chrome version", 30 | "last 1 firefox version", 31 | "last 1 safari version" 32 | ] 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxlow/react-router-layout-guide/a4165cff3bcbb494b805592652e17aa1d7aa0beb/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 25 | React Routing Guide 26 | 27 | 28 | 29 | 30 |
31 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxlow/react-router-layout-guide/a4165cff3bcbb494b805592652e17aa1d7aa0beb/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxlow/react-router-layout-guide/a4165cff3bcbb494b805592652e17aa1d7aa0beb/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Router, Route, Switch, Redirect } from "react-router-dom"; 3 | import { createBrowserHistory } from "history"; 4 | 5 | // Layouts & Route 6 | import routes from "./routes"; 7 | import PublicLayout from "./shared/layout/PublicLayout"; 8 | import AuthLayout from "./shared/layout/AuthLayout"; 9 | 10 | // Public pages 11 | import LoginPage from "./pages/LoginPage"; 12 | 13 | // Authenticated pages 14 | import DashboardPage from "./pages/DashboardPage"; 15 | // Uncomment below to use a custom 404 page 16 | // import NotFoundPage from "./pages/NotFoundPage"; 17 | 18 | const pages = [ 19 | // Public pages 20 | { 21 | exact: true, 22 | path: routes.login, 23 | component: LoginPage, 24 | layout: PublicLayout 25 | }, 26 | // Authenticated pages 27 | { 28 | exact: false, 29 | path: routes.dashboard, 30 | component: DashboardPage, 31 | layout: AuthLayout 32 | } 33 | ]; 34 | 35 | const App = () => { 36 | const history = createBrowserHistory(); 37 | 38 | return ( 39 | 40 | 41 | {pages.map( 42 | ({ exact, path, component: Component, layout: Layout }, index) => ( 43 | ( 48 | 49 | 50 | 51 | )} 52 | /> 53 | ) 54 | )} 55 | 56 | {/* Or Uncomment below to use a custom 404 page */} 57 | {/* */} 58 | 59 | 60 | ); 61 | }; 62 | 63 | export default App; 64 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 4 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 12 | monospace; 13 | } 14 | 15 | .public-container { 16 | height: 100vh; 17 | background-image: url("./shared/assets/images/mountain_background.png"); 18 | background-size: cover; 19 | } 20 | 21 | .primary-btn:focus, 22 | .primary-btn:hover, 23 | .primary-btn:active { 24 | color: white; 25 | border-color: white; 26 | } 27 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "antd/dist/antd.css"; 4 | import "./index.css"; 5 | 6 | import App from "./App"; 7 | import * as serviceWorker from "./serviceWorker"; 8 | 9 | ReactDOM.render(, document.getElementById("root")); 10 | 11 | // If you want your app to work offline and load faster, you can change 12 | // unregister() to register() below. Note this comes with some pitfalls. 13 | // Learn more about service workers: https://bit.ly/CRA-PWA 14 | serviceWorker.unregister(); 15 | -------------------------------------------------------------------------------- /src/pages/DashboardPage/DashboardView.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Alert, Row, Button } from "antd"; 3 | 4 | import routes from "../../routes"; 5 | 6 | const DashboardView = props => { 7 | const goToSettingsPage = () => { 8 | props.history.push(routes.settings); 9 | }; 10 | const goToStatisticsPage = () => { 11 | props.history.push(routes.statistics); 12 | }; 13 | return ( 14 |
15 | {/* Title of page */} 16 | 21 | 27 | 28 | 29 | {/* Navigation to nested routes */} 30 | 31 | 34 | 35 | 36 |
37 | ); 38 | }; 39 | 40 | export default DashboardView; 41 | -------------------------------------------------------------------------------- /src/pages/DashboardPage/SettingsView.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Button } from "antd"; 3 | 4 | const SettingsView = props => { 5 | const goBack = () => { 6 | props.history.goBack(); 7 | }; 8 | return ( 9 |
10 |

Settings View

11 | 12 |
13 | ); 14 | }; 15 | 16 | export default SettingsView; 17 | -------------------------------------------------------------------------------- /src/pages/DashboardPage/StatisticsView.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Button } from "antd"; 3 | 4 | const StatisticsView = props => { 5 | const goBack = () => { 6 | props.history.goBack(); 7 | }; 8 | return ( 9 |
10 |

Statistics View

11 |

{`props.loggedInUser.name --> ${props.loggedInUser.name}`}

12 | 13 |
14 | ); 15 | }; 16 | 17 | export default StatisticsView; 18 | -------------------------------------------------------------------------------- /src/pages/DashboardPage/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Switch } from "react-router-dom"; 3 | 4 | import RouteWithProps from "../../shared/routes/RouteWithProps"; 5 | import routes from "../../routes"; 6 | 7 | import DashboardView from "./DashboardView"; 8 | import SettingsView from "./SettingsView"; 9 | import StatisticsView from "./StatisticsView"; 10 | 11 | const DashboardPage = () => { 12 | const user = { name: "Hello world" }; 13 | return ( 14 | 15 | 16 | 17 | 23 | 24 | ); 25 | }; 26 | 27 | export default DashboardPage; 28 | -------------------------------------------------------------------------------- /src/pages/LoginPage.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Card, Form, Input, Button } from "antd"; 3 | import routes from "../routes"; 4 | 5 | const LoginPage = props => { 6 | const { getFieldDecorator } = props.form; 7 | const handleSubmit = () => { 8 | // Set token into localstorage 9 | localStorage.setItem("token", "I am now logged in"); 10 | props.history.push(routes.dashboard); 11 | }; 12 | return ( 13 | <> 14 | 22 |

React routing guide

23 |
29 | 30 | {getFieldDecorator("email", { 31 | rules: [{ required: true, message: "Email required" }] 32 | })()} 33 | 34 | 35 | {getFieldDecorator("password", { 36 | rules: [{ required: true, message: "Password required" }] 37 | })( 38 | 43 | )} 44 | 45 | 48 |
49 |
50 | 51 | ); 52 | }; 53 | 54 | export default Form.create({ name: "Login" })(LoginPage); 55 | -------------------------------------------------------------------------------- /src/pages/NotFoundPage.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | const NotFoundPage = () => { 4 | return

Page not found

; 5 | }; 6 | 7 | export default NotFoundPage; 8 | -------------------------------------------------------------------------------- /src/routes.js: -------------------------------------------------------------------------------- 1 | export default { 2 | // Roots 3 | home: "/", 4 | login: "/login", 5 | dashboard: "/dashboard", 6 | // Nested Dashboard pages 7 | statistics: "/dashboard/statistics", 8 | settings: "/dashboard/settings" 9 | }; 10 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.1/8 is considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl) 104 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/shared/assets/images/mountain_background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marxlow/react-router-layout-guide/a4165cff3bcbb494b805592652e17aa1d7aa0beb/src/shared/assets/images/mountain_background.png -------------------------------------------------------------------------------- /src/shared/layout/AuthLayout.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { message, Row, Layout, Button, Dropdown, Icon, Menu } from "antd"; 3 | import routes from "../../routes"; 4 | 5 | const { Header, Content } = Layout; 6 | 7 | class AuthLayout extends React.Component { 8 | componentDidMount() { 9 | if (!localStorage.getItem("token")) { 10 | // User is not logged in. Redirect back to login 11 | this.props.history.push(routes.login); 12 | message.warning("Please login first"); 13 | return; 14 | } 15 | // Fetch data for logged in user using token 16 | } 17 | 18 | onLogout = () => { 19 | // Remove token & other stored data 20 | localStorage.clear(); 21 | this.props.history.push(routes.login); 22 | }; 23 | 24 | render() { 25 | return ( 26 | 27 |
35 | 41 | {/* Dropdown with option to logout */} 42 | 45 | 46 | 47 | Logout 48 | 49 | 50 | } 51 | trigger={["click"]} 52 | > 53 | 56 | 57 | 58 |
59 | 60 | 61 | {this.props.children} 62 | 63 |
64 | ); 65 | } 66 | } 67 | 68 | export default AuthLayout; 69 | -------------------------------------------------------------------------------- /src/shared/layout/PublicLayout.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Row } from "antd"; 3 | 4 | const PublicLayout = props => { 5 | return ( 6 |
7 |
8 | 14 | {props.children} 15 | 16 |
17 |
18 | ); 19 | }; 20 | 21 | export default PublicLayout; 22 | -------------------------------------------------------------------------------- /src/shared/routes/RouteWithProps.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Route } from "react-router-dom"; 3 | 4 | const RouteWithProps = ({ exact, path, extraProps, component: Component }) => { 5 | return ( 6 | { 10 | const allProps = { ...props, ...extraProps }; 11 | return ; 12 | }} 13 | /> 14 | ); 15 | }; 16 | 17 | export default RouteWithProps; 18 | --------------------------------------------------------------------------------