2017-03-14 50 views
0
import ViewPager from 'react-native-viewpager'; 
constructor(props){ 
     super(props); 

     this._renderPage = this._renderPage.bind(this); 
     this._renderRowSablon = this._renderRowSablon.bind(this); 
     this.PageChange = this.PageChange.bind(this); 
     this.count = 0; 
     this.state = { 
       count: 0, 
       info : this.props.values, 
       page: 0, 
       pages:pages, 
     } 
} 
PageChange(x){ 
     switch(x){ 
       case 'next': 
        if(this.state.count< (this.state.info.sayfa - 1)){ 
          this.viewpager.goToPage(this.state.count + 1); 
          this.setState({count: this.state.count+ 1}); 
        } 
       break; 
     } 
    } 
render(){ 
      <View style={{flex:1}}> 
       <ViewPager 
        ref={(viewpager) => {this.viewpager = viewpager}} 
        dataSource={this.state.dataSource} 
        renderPage={this._renderPage} 
        onChangePage={this._pageChange} 
        isLoop={false} 
        renderPageIndicator={false} 
        locked={true} 
        autoPlay={false}/> 
      </View> 
      <View style={{flex:1}}> 
       <TouchableHighlight onPress={() => { this.PageChange('next'); }}> 
       <Text>Next</Text> 
        </TouchableHighlight> 
      </View> 
} 

当setState函数被清除时,页面正在发生更改。当添加setState函数(如上所述)时,setState工作,但页面更改(gotoPage)不起作用。不显示错误/警告究竟是什么问题?React-Native ViewPager goToPage不工作

+0

这将是有益的,如果你能提供更多的信息。像更多的代码,你尝试过的东西等等。我想看看你的状态声明在构造函数中。你是否在进口任何课程?你说的实际页面在哪里?这是android还是ios? –

+0

我这样说,因为我觉得你的问题是由于它的含糊不清而接受了很多的降薪。 –

+0

看到这个关于记录错误的问题:http://stackoverflow.com/questions/30115372/how-to-do-logging-in-react-native –

回答

0

看来你超出了范围。当使用箭头函数时,“this”的范围变成词法。

尝试改变<TouchableHighlight onPress={() => { this.PageChange('next'); }}>

<TouchableHighlight onPress={this.PageChange('next')}>

看到这个问题: React-Native: Cannot access state values or any methods declared from renderRow

+0

我认为这个问题会以这种方式解决。我们如何定义this.ViewPager?显示的错误是:undefined不是一个对象'this.viewpager.goToPage' –

+0

检查答案在这里:http://stackoverflow.com/questions/35663375/how-to-pass-a-component-reference-to- onpress-callback 您需要将'this'对象传递给函数。 而你的PassChange函数需要看起来像这样:'PageChange(x,this){' –

+1

感谢Adam。第一个问题已被纠正。但我仍然没有解决viewPager的问题。 –