Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

currentIndex not set when received new props #46

Closed
msn0 opened this issue Apr 19, 2016 · 2 comments
Closed

currentIndex not set when received new props #46

msn0 opened this issue Apr 19, 2016 · 2 comments

Comments

@msn0
Copy link

msn0 commented Apr 19, 2016

Hi,

I noticed the following code has been removed some time ago

componentWillReceiveProps(nextProps) {
  if (this.props.items.length !== nextProps.items.length) {
    const startIndex = nextProps.startIndex || 0
    this.setState({currentIndex: startIndex})
  }
}

Right now, when new props arrive, currentIndex stays as before. Hence when nextProps has more items than before, the gallery shows no image. I guess currentIndex should be reset in this case or is there some other way to do it right now?

@xiaolin
Copy link
Owner

xiaolin commented Apr 19, 2016

Hi, this was discussed in issue #36

The solution is using the slideToIndex function

You can do so by adding a ref to the ImageGallery component

e.g.

class MyComponent extends extends React.Component {

  _shouldResetGalleryIndex(nextProps) {
    // Your logic goes here
  }

  componentWillReceiveProps(nextProps) {
    if (this._shouldResetGalleryIndex(nextProps) && this._imageGallery) {
      this._imageGallery.slideToIndex(0)
    }
  }

  render() {
    <ImageGallery
      ref={i => this._imageGallery = i}
      items={this.props.items}
    />
  }
}

@msn0
Copy link
Author

msn0 commented Apr 19, 2016

Thanks, I did as you say and works perfectly. Thanks ;)

@msn0 msn0 closed this as completed Apr 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants