@@ -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 ( ) => {
0 commit comments