react-native-viewpager
react-native-viewpager copied to clipboard
This does not work when I request image data from a remote location.
` 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 (
<View style={styles.wrapper}>
<ViewPager
style={{height:130}}
dataSource={this.state.dataSource}
renderPage={this._renderPage}
isLoop={true}
autoPlay={true}/>
</View>
)
} `
then, have error:
Cannot read property 'pageIdentities of null
how can i resolve?
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.
it isn't work. please write a simple can you?
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