question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic Swiper Index Prop Fails on Android

See original GitHub issue

Currently trying to render an initial swiper page using the following code:

render() {
    const deviceWidth = Dimensions.get('window').width
    const deviceHeight = Dimensions.get('window').height

    return (
      <Swiper showsButtons loop = { false } index = { this.state.key }>
        { this.state.items.map((item) => {
          return (
            <View key = { item.id }>
              <Image
                style = {{ width: deviceWidth, height: deviceHeight }}
                resizeMode = 'contain'
                source = {{ uri: item.photo.image_url }}
              />
            </View>
          )
        })}
      </Swiper>
    )
  }

Where this.state.key is the index of a photo when pressed on a grid (going from grid -> full screen, hence why I want this image to be rendered first).

The following code works as it should on iOS, but I’m not having any luck on Android. If someone wants to point me in the right direction, I can look into submitting a PR.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:10

github_iconTop GitHub Comments

1reaction
evancloutiercommented, Mar 7, 2017

@pgonzalez-santiago try adding a componentDidMount() method to your component. I had myrender(), componentDidMount() and constructor set up like this:

constructor(props) {
    super(props)

    // Initializing the state attributes to be the appropriate data types
    this.state = {
      page: '',
      key: '',
      items: [],
    }
  }

  componentWillMount() {
    const items = this._transformPhotoArray(this.props.navigation.state.params.array)

    // Note that I'm updating the state here
    this.setState({
      page: this.props.navigation.state.params.page,
      key: this.props.navigation.state.params.key,
      items: items
    })
  }

  render() {
    const navigationParams = this.props.navigation.state.params

    return (
      <View style = { styles.container }>
        <View style = { styles.swiperContainer }>
          <Swiper
            loop = { false }
            height = { height }
            index = { this.state.page == 1 ? this.state.key : (this.state.key) + ((this.state.page - 1) * 20) }
            onMomentumScrollEnd = { this._onMomentumScrollEnd.bind(this) }
            renderPagination = { this._renderPagination.bind(this) }
            renderNewItems = { this._renderNewItems.bind(this) }
            fetchNextPage = { this._fetchNextPage.bind(this) }>
            { this.state.items.map((item, key) => {
              return (
                <View key = { key } style = { styles.imageContainer }>
                  <Image resizeMode = 'contain' style = { styles.image } source = {{ uri: item.photo.image_url }}/>
                </View>
              )
            })}
          </Swiper>
        </View>
        <PhotoFooter
          avatar = { navigationParams.avatar }
          photographer = { navigationParams.photographer }
          created = { navigationParams.created }
          views = { navigationParams.views }
          votes = { navigationParams.votes }
          >
        </PhotoFooter>
      </View>

    )
  }

I believe the issue is that the index prop of the Swiper isn’t being updated fast enough when it comes to the Android platform, and is defaulting to a value of zero.

Note that I have a bit more going on here in my render() than you probably need - I was adding a custom footer component to the bottom of the swiper.

Hope this helps.

1reaction
cj4luvcommented, Feb 10, 2017

I try to solve this problem…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dot does not work properly while rendering dynamic array ...
Steps to reproduce. set pagingEnabled true; use map to render screens inside a Swiper; try to swipe the screens and the active dot...
Read more >
react-native-swiper add child dynamically - Stack Overflow
I want to add dynamically component in <Swiper> ? ... [index]) return ( <Swiper horizontal={true} loop={false} onIndexChanged={(i)=>{ if (i ...
Read more >
Swiper Changelog
3.0.8 - Released on June 14th, 2015. Fixed issue with wrong active index and callbacks in Fade effect; New mousewheel parameters:.
Read more >
react swiper active index | The AI Search Engine You Control
you have to set the initialSlide prop on the Swiper element to an index of the slide in the middle. so that the...
Read more >
Custom View Components | Android Developers
If you fail to call this method from an overridden onMeasure() method, the result will be an exception at measurement time. At a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found