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

onIndexChanged this.setState({}) Loop not work #569

Open
jlemoscds opened this issue Sep 14, 2017 · 16 comments
Open

onIndexChanged this.setState({}) Loop not work #569

jlemoscds opened this issue Sep 14, 2017 · 16 comments

Comments

@jlemoscds
Copy link

jlemoscds commented Sep 14, 2017

Which OS

ios 10.3
android 6.0

Version

Which versions are you using:

  • react-native-swiper v1.5.12
  • react-native v0.47.2

Expected behaviour

Actual behaviour

On Android and IOS I can only swipe 1 to the left from 0 and no more (4/4). And swipe 4 to the right and no more (1/4).
The problems here is for some reason the loop brake in a strange way.
I try different things. And I found the problem is cause by this.setState in the onIndexChanged function which if I don't use it, the loop works.

How to reproduce it>

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react'
import {
  Text,
  View,
  Image,
  Dimensions,
  AppRegistry
} from 'react-native'
import Swiper from 'react-native-swiper'
const { width } = Dimensions.get('window')

const styles = {
  wrapper: {
  },
  slide: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: 'transparent'
  },
  text: {
    color: '#fff',
    fontSize: 30,
    fontWeight: 'bold'
  },
  image: {
    width,
    flex: 1
  },
  paginationStyle: {
    position: 'absolute',
    bottom: 10,
    right: 10
  },
  paginationText: {
    color: 'white',
    fontSize: 20
  }
}

var imageName = './img/1.jpg';
var originalName = 'Aussie tourist dies at Bali hotel';


const renderPagination = (index, total, context) => {
  return (
    <View style={styles.paginationStyle}>
      <Text style={{ color: 'grey' }}>
        <Text style={styles.paginationText}>{index + 1}</Text>/{total}
      </Text>
    </View>
  )
}

export default class AwesomeProject extends Component {
  constructor(props) {
    super(props);
    this.state = {
      renderArray: [true, false, false, false]
    };
  }


  render() {
    return (
      <Swiper
        style={styles.wrapper}
        onIndexChanged={index => this.indexChanged(index)} 
        renderPagination={renderPagination}
        loop={true}
      >

        <View style={styles.slide} title={<Text numberOfLines={1}>{'Aussie tourist dies at Bali hotel'}</Text>}>
          {this.state.renderArray[0] ?
            <Image style={styles.image} source={require('./img/1.jpg')} />
            : null}
        </View>
        <View style={styles.slide} title={<Text numberOfLines={1}>Big lie behind Nine’s new show</Text>}>
          {this.state.renderArray[1] ?
            <Image style={styles.image} source={require('./img/2.jpg')} />
            : null}
        </View>
        <View style={styles.slide} title={<Text numberOfLines={1}>Why Stone split from Garfield</Text>}>
          {this.state.renderArray[2] ?
            <Image style={styles.image} source={require('./img/3.jpg')} />
            : null}
        </View>
        <View style={styles.slide} title={<Text numberOfLines={1}>Learn from Kim K to land that job</Text>}>
          {this.state.renderArray[3] ?
            this.renderImage4()
            : null}
        </View>
      </Swiper>
    )
  }

  renderImage4(){
    return <Image style={styles.image} source={require('./img/4.jpg')} />;

  }


  indexChanged(index) {
    var tempvar = this.state.renderArray;
    tempvar[index] = true
    this.setState({ renderArray: tempvar}); //<<======== problem with this
  }
}



AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);

Steps to reproduce

  1. use setState onIndexChanged and loop works weird .
@jlemoscds jlemoscds changed the title onIndexChanged this.setState({}). onIndexChanged this.setState({}) Loop not work Sep 14, 2017
@tquiroga
Copy link

Exactly the same problem for me on "react-native-swiper": "^1.5.12"

@HuiSF
Copy link

HuiSF commented Sep 28, 2017

+1 the same problem.
I observed that if call setState in parent component, componentWillReceiveProps of the swiper component gets triggered, and nextProps is exactly as the same as props of current swiper component. And further setState and initState of the swiper component get called, so the swiper state gets messed up.

I temporarily added if (nextProps.index === this.props.index) return; in componentWillReceiveProps to solve this issue.

@sourcesoft
Copy link

sourcesoft commented Oct 2, 2017

Having the same problem but I'm actually dispatching actions. I think it's almost related to #503

  handleIndexChanged = (e, { index }) => {
    if (index === 0 || index === 2) {
      this.props.dispatch(actions.setHeadShow(false));
    } else {
      this.props.dispatch(actions.setHeadShow(true));
    }
  };

@freiserg
Copy link

freiserg commented Oct 9, 2017

@arribbar I had so probiems after this commit 1fb7ff2

I removed this code and it works.

@SirNeuman
Copy link

I'm also having this problem with version "react-native-swiper": "^1.5.13". Using redux to dispatch actions using onIndexChanged. If my component is not listening to the prop which gets updated by this action then the swiper appears to behave normally.

jinglinwishlife added a commit to jinglinwishlife/react-native-swiper that referenced this issue May 2, 2018
@sizovilya
Copy link

When planned new npm version with this fix ?

@roysG
Copy link

roysG commented Sep 8, 2018

Still happening in version: 1.5.13
For not i added the temporary solution of @HuiSF
When it will be fixed?

@jeppe-smith
Copy link

Also having this issue. I found that using setTimeout solves the issue.

@martabacc
Copy link

hi @jeppe-smith , can I see how you implement the setTimeout part?

@EdgarXolop
Copy link

@ronayumik

...
loop={false}
onIndexChanged={(swipe_index)=>{
  setTimeout(()=>this.setState({swipe_index}),200)
}}
...

that works for me, the this.setState doesn't work with loop={true}

@martabacc
Copy link

martabacc commented Nov 21, 2018

@EdgarXolop , do you have any idea why it needs the timeout? is it the rendering part that takes too long?

deepakaggarwal7 added a commit to deepakaggarwal7/react-native-swiper that referenced this issue Feb 23, 2019
Solves the issue of state messing up when parent component calls setState [ref leecade#569]
@fahmidme
Copy link

For me, setting loop={false} worked.

@CVRamana
Copy link

@ronayumik

...
loop={false}
onIndexChanged={(swipe_index)=>{
  setTimeout(()=>this.setState({swipe_index}),200)
}}
...

that works for me, the this.setState doesn't work with loop={true}
it also removes the warning of componentwillreceive prop

@sebastianupr
Copy link

@arribbar I had so probiems after this commit 1fb7ff2

I removed this code and it works.

THANKSS

@zOok91
Copy link

zOok91 commented Jul 28, 2021

setTimeout seemed to fix the warning Warning: Cannot update a component from inside the function body of a different component. for me but the second slide style was breaking for me. When I swipe to the second slide, it would be offset to the left and only on clicking it, it would align right.
After a lot of trial and error, adding loadMinimal={true} is what did the trick. loop was still set to true.

@gavrielk
Copy link

@arribbar I had so probiems after this commit 1fb7ff2

I removed this code and it works.

It did the magic, thanks!
Anyone knows if or when? this is going to be integrated to the official build?

rauljurado620 added a commit to rauljurado620/react-native-swiper that referenced this issue Jun 15, 2023
Solves the issue of state messing up when parent component calls setState [ref leecade/react-native-swiper#569]
mapleleaf5522 pushed a commit to mapleleaf5522/react-native-swiper that referenced this issue Dec 11, 2024
Solves the issue of state messing up when parent component calls setState [ref leecade/react-native-swiper#569]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests