-
-
Save jgcmarins/931762ae437f09f980a3aa1a8a711cae to your computer and use it in GitHub Desktop.
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
| // ---- routes.ts --- | |
| import * as React from 'react'; | |
| import { prepareQuery } from '../../relay'; | |
| export default [ | |
| { | |
| path: path => `${path}Home`, | |
| component: React.lazy(() => import('./Home')), | |
| prepare: () => { | |
| const query = require('./__generated__/HomeQuery.graphql'); | |
| return prepareQuery(query); | |
| }, | |
| }, | |
| ]; | |
| //----- LazyRoute.tsx --- | |
| import * as React from 'react'; | |
| import { Route, RouteProps } from 'react-router-dom'; | |
| interface Props extends RouteProps { | |
| component: React.ComponentType<{ preloadedQuery: any }>; | |
| loadingComponent?: React.ComponentType; | |
| prepare?: () => { query: any }; | |
| } | |
| const LazyRouter = ({ | |
| prepare, | |
| component: Component, | |
| loadingComponent: Loading, | |
| ...props | |
| }: Props) => { | |
| const { query } = prepare(); | |
| return ( | |
| <Route {...props}> | |
| <React.Suspense fallback={<Loading />}> | |
| <Component preloadedQuery={query} /> | |
| </React.Suspense> | |
| </Route> | |
| ); | |
| }; | |
| export default LazyRouter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment