Last active
January 20, 2019 01:35
-
-
Save baetheus/23944dc671caecf1508db94581000f7d to your computer and use it in GitHub Desktop.
React Redux Router POC
This file contains hidden or 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 * as React from 'react'; | |
| import { SFC } from 'react'; | |
| import { useRedux } from '~/store'; | |
| import { currentRoute, Routes } from '~/stores/routing'; | |
| import { Campaign } from './pages/Campaign'; | |
| import { Dashboard } from './pages/Dashboard'; | |
| export interface RouterProps {} | |
| /** | |
| * @render react | |
| * @name Router | |
| * @example | |
| * <Router text="Hello World" /> | |
| */ | |
| export const Router: SFC<RouterProps> = ({}) => { | |
| const [route] = useRedux(currentRoute); | |
| // Route from store | |
| switch (route.route) { | |
| case Routes.CAMPAIGN: | |
| return <Campaign {...route.params} />; | |
| case Routes.DASHBOARD: | |
| return <Dashboard />; | |
| default: | |
| return <div>Unknown Route</div>; | |
| } | |
| }; |
This file contains hidden or 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
| export enum Routes { | |
| DASHBOARD, | |
| CAMPAIGN, | |
| PROSPECTS, | |
| } | |
| interface Route<R extends Routes, T = unknown> { | |
| route: R; | |
| params?: T; | |
| } | |
| type RouteP<R extends Routes, T> = Required<Route<R, T>> | |
| type DashboardRoute = Route<Routes.DASHBOARD>; | |
| type CampaignRoute = RouteP<Routes.CAMPAIGN, { campaignId: number }>; | |
| export type AccessibleRoutes = DashboardRoute | CampaignRoute; | |
| export interface RouterStore { | |
| currentRoute: AccessibleRoutes; | |
| } | |
| export const ROUTER_STORE_KEY = 'ROUTER'; | |
| export const INIT_ROUTER_STORE: RouterStore = { | |
| currentRoute: { route: Routes.DASHBOARD, params: {} }, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment