race604 / react-native-viewpager

[Deprecated] ViewPager component for React Native
1.41k stars 373 forks source link

This does not work when I request image data from a remote location. #121

Open luojinghui opened 7 years ago

luojinghui commented 7 years ago

` constructor(props) { super(props);

this.state = {
  jnSlides: [],
  dataSource: null
};

} componentDidMount() { this._isMounted = true; let uri = 'RotationImages/banner'; let _this = this;

this.req = jnRequest.getData(uri, (res) => {
  var dataSource = new ViewPager.DataSource({
    pageHasChanged: (p1, p2) => p1 !== p2,
  });
  var setIt = dataSource.cloneWithPages(res);

  if (this._isMounted) {
    _this.setState({
      jnSlides: res,
      dataSource: setIt
    })
  }
})

} _renderPage(data, pageID) { console.log(data); return ( <Image source={data.image} style={styles.slide1}/> ); } render() { return (

)

} `

then, have error: Cannot read property 'pageIdentities of null how can i resolve?

Fulla commented 7 years ago

start by setting dataSource = [] instead of null in constructor. I don't know if it'll work if you do as I say, but I'm pretty sure it fails if you don't.

lizhuoyuan commented 7 years ago

it isn't work. please write a simple can you?

mateofd commented 7 years ago

Why don't you do this:

var dataSource = new ViewPager.DataSource({
    pageHasChanged: (p1, p2) => p1 !== p2,
  });

In the constructor and then assign it to state.

Then in the appropriate section of componentDidMount try this:

if (this._isMounted) {
    _this.setState({
      jnSlides: res,
      dataSource: dataSource.cloneWithPages(res)
    })
  }

Hope it works