In my project I used to rely on react-loadable to do some code spliting, lazy loading together with react-router. Something like:
<Route component={component} />
with
Component = Loadable({
loader: () => import(/* webpackChunkName: "Analyze" */ "./components/Analyze"),
})
The Analyze component implements componentDidMount
and use router's history.push
on state change. When a new url is pushed with some parameter changed, but still leading to this same "Analyze" component, only componentDidUpdate
is called. I updated this code to use React.lazy:
<Route component={(_props: any) =>
<LazyComponentWrapper>
<Component {..._props} />
</LazyComponentWrapper>
} />
with
Component = React.lazy(() => import(/* webpackChunkName: "Analyze" */ "./components/Analyze")),
and
function LazyComponentWrapper({ children }) {
return (
<Suspense fallback={<div>{LOADING}</div>}>
{children}
</Suspense>
);
but now componentDidMound
is unexpectedly called every time. It's not clear to me wether this have to do with React.lazy
or with react-router. Any clue ?