react-native-swiper icon indicating copy to clipboard operation
react-native-swiper copied to clipboard

Issue with index for loading initial slide when using map function

Open ivanovzlatan opened this issue 7 years ago • 6 comments

// When I use index={2} here, the slider shows as Active-dot the third slide, but renders content from the first slide.

  renderContent() {
      return (
       <Swiper loop={false} index={2}>
          {this.renderAlbums()}
        </Swiper>
      );
    }

Image:
screenshot_20170401-212814

If I use the same index={2}, without MAP, but with hard-code - It renders as Active-dot the third one, and renders the content from the third slide. Which is correct. When using this:

   renderContent() {
     <Swiper loop={false} index={2}>
        <View><Text>Page 1</Text></View>
        <View><Text>Page 2</Text></View>
        <View><Text>Page 3</Text></View>
      </Swiper>
      );
    }
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import Swiper from 'react-native-swiper';

class AlbumList extends Component {
  state = { albums: ['Page 1', 'Page 2', 'Page 3', 'Page 4'] };

  renderAlbums() {
    return this.state.albums.map(album =>
      <View><Text>{album}</Text></View>
    );
  }

// When I use index={2} here, the slider shows as Active-dot the third slide, but renders content from the first slide.
  renderContent() {
      return (
       <Swiper loop={false} index={2}>
          {this.renderAlbums()}
        </Swiper>
      );
    }

/* If I use the same index={2}, without MAP, but with hard-code - It renders as Active-dot the third one, and renders the content from the third slide. Which is correct. When using this:
   renderContent() {
     <Swiper loop={false} index={2}>
        <View><Text>Page 1</Text></View>
        <View><Text>Page 2</Text></View>
        <View><Text>Page 3</Text></View>
      </Swiper>
      );
    }
    
  
*/
  render() {
    return (
        <View>
          {this.renderContent()}
        </View>
    );
  }
}

export default AlbumList;

ivanovzlatan avatar Apr 02 '17 11:04 ivanovzlatan

same problem for me: <Swiper index={this.state.swiperPosition} loop={false} showsPagination={false} loadMinimal> {this.state.imagePosts.map((i, k, arr) => this._renderImage(i, k, arr))} </Swiper>

YvanGuidoin avatar May 08 '17 22:05 YvanGuidoin

+1 using map statement

stefensuhat avatar May 10 '17 15:05 stefensuhat

+1. cant imagine what the hell causes this.

stokesbga avatar May 12 '17 03:05 stokesbga

Same for me

AtticusFetch avatar Aug 13 '17 15:08 AtticusFetch

After a bit of investigation I discovered that, for ScrollView, which is rendered for iOS, contentOfsset is undefined.

onLayout = (event) => {

    ...
   // At this point, this.state.width and this.state.height equal values from Dimensions.get('window')
    if (width !== this.state.width || height !== this.state.height) {
      state.offset = offset   <---- This never happens
    }
    this.setState(state) 
}

So, for my case, when swipers width equals to Dimensions.get('window').width, a quick hack to fix is would be to simply set width={myWidth - 1} as swiper prop, which will allow to set offset inside onLayout. But a better way would be to check whether this.state.offset is defined

AtticusFetch avatar Aug 13 '17 15:08 AtticusFetch

i am having the same issue anyone solved it...?

AbdullahJaspal avatar Oct 04 '21 22:10 AbdullahJaspal