React-native-swiper: Android - Dynamic Pages Index Problems

Created on 30 Nov 2016  ·  2Comments  ·  Source: leecade/react-native-swiper

When dynamically loading pages in componentDidMount, if the new array of pages has more than what was set in the constructor you cannot scroll to these pages. The following will reproduce the bug:

  constructor(props) {
    super(props)

    this.state = {
      posts: [{
        id: 'FOO'
      },
      {
        id: 'BAR'
      }]
    }
  }

  componentDidMount() {
    this.setState({
      posts: [{
        id: 'FOO'
      },
      {
        id: 'BAR'
      },
      {
        id: 'FOOBAR'
      },
      {
        id: 'BARFOO'
      }]
    })
  }

This works fine:

  constructor(props) {
    super(props)

    this.state = {
      posts: [{
        id: 'FOO'
      },
      {
        id: 'BAR'
      }]
    }
  }

  componentDidMount() {
    this.setState({
      posts: [{
        id: 'FOOBAR'
      },
      {
        id: 'BARFOO'
      }]
    })
  }

Using React Native 0.38

Most helpful comment

I came across this issue also. Looks like its due to https://github.com/facebook/react-native/issues/4775

The quick fix would be to add this prop to swiper: key={this.state.posts.length}

All 2 comments

I came across this issue also. Looks like its due to https://github.com/facebook/react-native/issues/4775

The quick fix would be to add this prop to swiper: key={this.state.posts.length}

@andyjamesdavies Thanks, it works for iOS too.

Was this page helpful?
0 / 5 - 0 ratings