Skip to content

Commit 19ca584

Browse files
committed
remove UNSAFE_componentWillReceiveProps
1 parent 9e5ac46 commit 19ca584

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/index.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface Props {
2929
interface State {
3030
showLoader: boolean;
3131
pullToRefreshThresholdBreached: boolean;
32+
prevDataLength: number | undefined;
3233
}
3334

3435
export default class InfiniteScroll extends Component<Props, State> {
@@ -38,6 +39,7 @@ export default class InfiniteScroll extends Component<Props, State> {
3839
this.state = {
3940
showLoader: false,
4041
pullToRefreshThresholdBreached: false,
42+
prevDataLength: props.dataLength,
4143
};
4244

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

140-
UNSAFE_componentWillReceiveProps(props: Props) {
142+
componentDidUpdate(nextProps: Props) {
141143
// do nothing when dataLength is unchanged
142-
if (this.props.dataLength === props.dataLength) return;
144+
if (this.props.dataLength === nextProps.dataLength) return;
143145

144146
this.actionTriggered = false;
145-
// update state when new data was sent in
146-
this.setState({
147-
showLoader: false,
148-
});
147+
}
148+
149+
static getDerivedStateFromProps(nextProps: Props, prevState: State) {
150+
const dataLengthChanged = nextProps.dataLength !== prevState.prevDataLength;
151+
152+
// reset when data changes
153+
if (dataLengthChanged) {
154+
return {
155+
showLoader: false,
156+
pullToRefreshThresholdBreached: false,
157+
prevDataLength: nextProps.dataLength,
158+
};
159+
}
160+
return null;
149161
}
150162

151163
getScrollableTarget = () => {

0 commit comments

Comments
 (0)