Skip to content
This repository was archived by the owner on Sep 28, 2021. It is now read-only.

Commit 55a460c

Browse files
authored
Merge pull request #358 from ArtemKolichenkov/update-deprecated-lifecycle-methods
Update deprecated lifecycle methods
2 parents e6b0adf + 169cbc3 commit 55a460c

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/image-viewer.component.tsx

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,29 @@ export default class ImageViewer extends React.Component<Props, State> {
4848

4949
private imageRefs: any[] = [];
5050

51-
public componentWillMount() {
51+
public componentDidMount() {
5252
this.init(this.props);
5353
}
5454

55-
public componentWillReceiveProps(nextProps: Props) {
56-
if (nextProps.index !== this.state.currentShowIndex) {
57-
this.setState(
58-
{
59-
currentShowIndex: nextProps.index
60-
},
61-
() => {
62-
// 立刻预加载要看的图
63-
this.loadImage(nextProps.index || 0);
64-
65-
this.jumpToCurrentImage();
66-
67-
// 显示动画
68-
Animated.timing(this.fadeAnim, {
69-
toValue: 1,
70-
duration: 200
71-
}).start();
72-
}
73-
);
55+
static getDerivedStateFromProps(nextProps: Props, prevState: State) {
56+
if (nextProps.index !== prevState.prevIndexProp) {
57+
return { currentShowIndex: nextProps.index, prevIndexProp: nextProps.index };
58+
}
59+
return null;
60+
}
61+
62+
public componentDidUpdate(prevProps: Props, prevState: State) {
63+
if (prevProps.index !== this.props.index) {
64+
// 立刻预加载要看的图
65+
this.loadImage(this.props.index || 0);
66+
67+
this.jumpToCurrentImage();
68+
69+
// 显示动画
70+
Animated.timing(this.fadeAnim, {
71+
toValue: 1,
72+
duration: 200
73+
}).start();
7474
}
7575
}
7676

@@ -97,6 +97,7 @@ export default class ImageViewer extends React.Component<Props, State> {
9797
this.setState(
9898
{
9999
currentShowIndex: nextProps.index,
100+
prevIndexProp: nextProps.index || 0,
100101
imageSizes
101102
},
102103
() => {

src/image-viewer.type.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ export class State {
229229
*/
230230
public currentShowIndex?: number = 0;
231231

232+
/**
233+
* Used to detect if parent component applied new index prop
234+
*/
235+
public prevIndexProp?: number = 0;
236+
232237
/**
233238
* 图片拉取是否完毕了
234239
*/

0 commit comments

Comments
 (0)