Skip to content

Commit 0389d1b

Browse files
authored
Fix equality check
Assuming the lazy-loaded component is used as: ```js render() { return ( <div> <LazilyLoad modules={{ TodoHandler: () => importLazy(import('./components/TodoHandler')), }}> {({TodoHandler}) => ( <TodoHandler /> )} </LazilyLoad> </div> ); } ``` The previous equality check called `this.load` each time the parent-component re-rendered, which remounted all the lazy-loaded components. Comparing the actual promise references instead of the modules object fixes this.
1 parent 9a135ca commit 0389d1b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

content/guides/lazy-load-react.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ class LazilyLoad extends React.Component {
8383
}
8484

8585
componentDidUpdate(previous) {
86-
if (this.props.modules === previous.modules) return null;
87-
this.load();
86+
const shouldLoad = !!Object.keys(this.props.modules).filter((key)=> {
87+
return this.props.modules[key] !== previous.modules[key];
88+
}).length;
89+
if (shouldLoad) {
90+
this.load();
91+
}
8892
}
8993

9094
componentWillUnmount() {

0 commit comments

Comments
 (0)