This commit is contained in:
Raluca Stan 2021-07-12 14:58:10 +03:00 committed by GitHub
parent ce965a3578
commit 4907ddcd5d
1 changed files with 22 additions and 12 deletions

View File

@ -47,6 +47,8 @@ const withReviews = ( OriginalComponent ) => {
delayedAppendReviews = this.props.delayFunction( this.appendReviews ); delayedAppendReviews = this.props.delayFunction( this.appendReviews );
isMounted = false;
state = { state = {
error: null, error: null,
loading: true, loading: true,
@ -57,6 +59,7 @@ const withReviews = ( OriginalComponent ) => {
}; };
componentDidMount() { componentDidMount() {
this.isMounted = true;
this.replaceReviews(); this.replaceReviews();
} }
@ -80,7 +83,9 @@ const withReviews = ( OriginalComponent ) => {
); );
} }
componentWillUnMount() { componentWillUnmount() {
this.isMounted = false;
if ( this.delayedAppendReviews.cancel ) { if ( this.delayedAppendReviews.cancel ) {
this.delayedAppendReviews.cancel(); this.delayedAppendReviews.cancel();
} }
@ -120,7 +125,6 @@ const withReviews = ( OriginalComponent ) => {
} }
const { onReviewsReplaced } = this.props; const { onReviewsReplaced } = this.props;
this.updateListOfReviews().then( onReviewsReplaced ); this.updateListOfReviews().then( onReviewsReplaced );
} }
@ -158,16 +162,19 @@ const withReviews = ( OriginalComponent ) => {
reviews: newReviews, reviews: newReviews,
totalReviews: newTotalReviews, totalReviews: newTotalReviews,
} ) => { } ) => {
this.setState( { if ( this.isMounted ) {
reviews: oldReviews this.setState( {
.filter( reviews: oldReviews
( review ) => Object.keys( review ).length .filter(
) ( review ) =>
.concat( newReviews ), Object.keys( review ).length
totalReviews: newTotalReviews, )
loading: false, .concat( newReviews ),
error: null, totalReviews: newTotalReviews,
} ); loading: false,
error: null,
} );
}
return { newReviews }; return { newReviews };
} }
@ -176,6 +183,9 @@ const withReviews = ( OriginalComponent ) => {
} }
setError = async ( e ) => { setError = async ( e ) => {
if ( ! this.isMounted ) {
return;
}
const { onReviewsLoadError } = this.props; const { onReviewsLoadError } = this.props;
const error = await formatError( e ); const error = await formatError( e );