Skip to content

Commit

Permalink
fix(fsproductindex): Fix infinite loop of requesting reviews
Browse files Browse the repository at this point in the history
Since the requestReviews method now alters the component's state rather
than triggering an update that results in new props being passed in, the
props/state commerceData in getDerivedStateFromProps were never equal.
This caused the subsequent componentDidUpdate call to always request a
new set of reviews.
  • Loading branch information
nathan-sankbeil authored and bweissbart committed Aug 1, 2018
1 parent f33a6b0 commit a8f0128
Showing 1 changed file with 4 additions and 25 deletions.
29 changes: 4 additions & 25 deletions packages/fsproductindex/src/components/ProductIndexProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,33 +132,12 @@ function withProductIndexData<
type ResultState = WithProductIndexState<ProductType, IdxType>;

class ProductIndexProvider extends Component<ResultProps, ResultState> {
static getDerivedStateFromProps(
nextProps: ResultProps,
prevState: ResultState
): Partial<ResultState> | null {
if (!isEqual(nextProps.commerceData, prevState.commerceData)) {
return {
commerceData: nextProps.commerceData,
commerceDataDirty: true
};
}

return null;
}

constructor(props: ResultProps) {
super(props);

this.state = {
commerceDataDirty: true
};
}

/**
* Request new reviews if commerce data is dirty
* Request new reviews if commerce data has changed
* @param {ResultProps} prevProps - Previously set Props
*/
componentDidUpdate(): void {
if (this.state.commerceDataDirty) {
componentDidUpdate(prevProps: ResultProps): void {
if (!isEqual(this.props.commerceData, prevProps.commerceData)) {
if (!this.props.disableReviews) {
this.requestReviews().catch(err => console.warn('Could not get reviews', err));
}
Expand Down

0 comments on commit a8f0128

Please sign in to comment.