Skip to content

Commit

Permalink
remove UNSAFE_componentWillReceiveProps
Browse files Browse the repository at this point in the history
  • Loading branch information
Newbie012 committed Nov 8, 2020
1 parent 9e5ac46 commit 19ca584
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Props {
interface State {
showLoader: boolean;
pullToRefreshThresholdBreached: boolean;
prevDataLength: number | undefined;
}

export default class InfiniteScroll extends Component<Props, State> {
Expand All @@ -38,6 +39,7 @@ export default class InfiniteScroll extends Component<Props, State> {
this.state = {
showLoader: false,
pullToRefreshThresholdBreached: false,
prevDataLength: props.dataLength,
};

this.throttledOnScrollListener = throttle(150, this.onScrollListener).bind(
Expand Down Expand Up @@ -137,15 +139,25 @@ export default class InfiniteScroll extends Component<Props, State> {
}
}

UNSAFE_componentWillReceiveProps(props: Props) {
componentDidUpdate(nextProps: Props) {
// do nothing when dataLength is unchanged
if (this.props.dataLength === props.dataLength) return;
if (this.props.dataLength === nextProps.dataLength) return;

this.actionTriggered = false;
// update state when new data was sent in
this.setState({
showLoader: false,
});
}

static getDerivedStateFromProps(nextProps: Props, prevState: State) {
const dataLengthChanged = nextProps.dataLength !== prevState.prevDataLength;

// reset when data changes
if (dataLengthChanged) {
return {
showLoader: false,
pullToRefreshThresholdBreached: false,
prevDataLength: nextProps.dataLength,
};
}
return null;
}

getScrollableTarget = () => {
Expand Down

0 comments on commit 19ca584

Please sign in to comment.