Skip to content

How to use library with code splitting with React.lazy and use declarative routing? #158

@fedulovivan

Description

@fedulovivan

Say with have several modules, one for each page. Each of them is pretty heavy, and its not an option to load them all in advance to prepare static routes map as demonstrated in example (const routes = [{ path: '/foo', component: Page1 },...];)

Each module contains async-connected page component + appropriate reducer.

first create lazy wrappers for each page:

const Page1 = lazy(async () => {
    const moduleBundle = await import(/* webpackChunkName: "Page1" */'src/pages/Page1');
    injectAsyncReducer(store, moduleBundle.constants.NAME, moduleBundle.reducer);
    return moduleBundle;
});
const Page2 = lazy(async () => {
    const moduleBundle = await import(/* webpackChunkName: "Page2" */'src/pages/Page3');
    injectAsyncReducer(store, moduleBundle.constants.NAME, moduleBundle.reducer);
    return moduleBundle;
});

injectAsyncReducer function re-configures store and injects reducer for certain page using replaceReducer redux utility.

now init provider, router, suspense-wrapper, define routes and mount tree:

ReactDOM.render(
    <Provider store={store}>
        <BrowserRouter>
            <GenericContainer>
                <App>
                    <Suspense fallback={<div>Loading...</div>}>
                        <Switch>
                            <Route path="/page1">
                                <Page1 />
                            </Route>
                            <Route path="/page2">
                                <Page2 />
                            </Route>
                        </Switch>
                    </Suspense>
                </App>
            </GenericContainer>
        </BrowserRouter>
    </Provider>,
    renderTarget,
);

so the question, where is the place for <ReduxAsyncConnect> component here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions