├── .gitignore ├── example ├── src │ ├── react-app-env.d.ts │ ├── setupTests.ts │ ├── App.test.tsx │ ├── index.css │ ├── Components │ │ ├── Route1 │ │ │ └── index.tsx │ │ ├── Unauthorized │ │ │ └── index.tsx │ │ ├── Route3 │ │ │ └── index.tsx │ │ └── Route2 │ │ │ └── index.tsx │ ├── index.tsx │ ├── routes.tsx │ ├── App.tsx │ ├── App.css │ ├── logo.svg │ └── serviceWorker.ts ├── public │ ├── robots.txt │ ├── favicon.ico │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── index.html ├── .gitignore ├── tsconfig.json ├── package.json └── README.md ├── src ├── index.ts ├── Routes │ └── index.tsx ├── Hooks │ └── useLocation.tsx ├── Context │ └── index.tsx ├── types.ts ├── PathUtils │ └── index.ts └── Router │ └── index.tsx ├── .vscode └── settings.json ├── package.json ├── LICENSE ├── tsconfig.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | -------------------------------------------------------------------------------- /example/src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /example/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /example/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hryuk/react-awesome-router/HEAD/example/public/favicon.ico -------------------------------------------------------------------------------- /example/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hryuk/react-awesome-router/HEAD/example/public/logo192.png -------------------------------------------------------------------------------- /example/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hryuk/react-awesome-router/HEAD/example/public/logo512.png -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export { Router } from './Router'; 2 | export { useLocation } from './Hooks/useLocation'; 3 | export { Routes } from './Routes'; 4 | export { Guard, Route } from './types'; 5 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "prettier.singleQuote": true, 3 | "editor.defaultFormatter": "esbenp.prettier-vscode", 4 | "prettier.printWidth": 100, 5 | "typescript.tsdk": "node_modules/typescript/lib" 6 | } -------------------------------------------------------------------------------- /src/Routes/index.tsx: -------------------------------------------------------------------------------- 1 | import React, {useContext, Fragment} from 'react'; 2 | import {RouterContext} from '../Context'; 3 | 4 | export const Routes: React.FC = () => { 5 | const {component} = useContext(RouterContext); 6 | 7 | return {component}; 8 | }; 9 | -------------------------------------------------------------------------------- /example/src/setupTests.ts: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /src/Hooks/useLocation.tsx: -------------------------------------------------------------------------------- 1 | import { useContext } from 'react'; 2 | import { RouterContext } from '../Context'; 3 | 4 | export const useLocation = () => { 5 | const { location, context, params, setLocation, setContext } = useContext(RouterContext); 6 | 7 | return { location, context, params, setLocation, setContext }; 8 | }; 9 | -------------------------------------------------------------------------------- /example/src/App.test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import {render} from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const {getByText} = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /src/Context/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export const RouterContext = React.createContext({ 4 | location: '', 5 | context: {} as any, 6 | params: {}, 7 | component: null as React.ReactNode | undefined | null, 8 | setLocation: (location: string) => { }, 9 | setContext: (context: Object) => { } 10 | }); 11 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | export interface Route { 2 | path: string; 3 | component: React.ReactNode; 4 | guards?: Array; 5 | } 6 | 7 | export interface Router { 8 | location: string; 9 | context: any; 10 | params: any; 11 | setLocation: (location: string) => void; 12 | setContext: (context: Object) => void; 13 | } 14 | 15 | export type Guard = (router: Router, next: () => undefined) => React.ReactNode; -------------------------------------------------------------------------------- /example/.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 | -------------------------------------------------------------------------------- /example/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 | -------------------------------------------------------------------------------- /example/src/Components/Route1/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | import {useLocation} from 'react-awesome-router'; 4 | 5 | const Route1: React.FC = () => { 6 | const {location, setLocation} = useLocation(); 7 | 8 | return ( 9 |
10 |
ROUTE 1
11 |