Created
March 2, 2020 21:38
-
-
Save gsasouza/bb365c9b9a72500b2acd2bd259e02115 to your computer and use it in GitHub Desktop.
This file contains 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