react-native-swiper 下一个/上一个按钮事件

标签 react-native swiper

swiper 索引值按页面顺序出现。
但是,当按下按钮时,索引值会上升到无穷大。

例如)下一个按钮点击-> 6/5、7/5、8/5

我想做的是在 5/5 停止按钮事件,但我不知道该怎么做。

https://github.com/leecade/react-native-swiper

App.js

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 App extends Component {

  constructor(props){
    super(props);
    this.onPressNext = this.onPressNext.bind(this);
    this.state = {
      idxActive: 1
    }
 }

  onPressPrev = () => {
    this.refs.swiper.scrollBy(-1)
  }

  onPressNext = () => {
    this.refs.swiper.scrollBy(1);
  }

  render() {
    return (

      <View style={styles.container}>


        <Swiper
          style={styles.wrapper}
          renderPagination={renderPagination}
          showsButtons={false}
          loop={false}
          ref={'swiper'}
        >
          <View style={styles.slide}>
            <Text style={styles.text}>1 page</Text>
          </View>
          <View style={styles.slide}>
            <Text style={styles.text}>2 page</Text>
          </View>
          <View style={styles.slide}>
            <Text style={styles.text}>3 page</Text>
          </View>
          <View style={styles.slide}>
            <Text style={styles.text}>4 page</Text>
          </View>
          <View style={styles.slide}>
            <Text style={styles.text}>5 page</Text>
          </View>
        </Swiper>

        <View style={styles.buttoncontainer}>
          <Button
            onPress={this.onPressPrev}
            title="previous">
          </Button>
          <Button
            onPress={this.onPressNext}
            title="next">
          </Button>

        </View>
      </View>
    );
  }
}

最佳答案

使用 onIndexedChanged刷机 Prop 获取最新index并将其保存在本地组件状态中。就像是:

export default class App extends Component {

  constructor(props){
    super(props);
    this.onPressNext = this.onPressNext.bind(this);
    this.onPressPrev = this.onPressPrev.bind(this);
    this.state = {
      idxActive: 0
    }
 }

  onPressPrev = () => {
    const {idxActive} = this.state;
    if (idxActive > 0) {
      this.refs.swiper.scrollBy(-1)
    }
  }

  onPressNext = () => {
    const {idxActive} = this.state;
    // Probably best set as a constant somewhere vs a hardcoded 5
    if (idxActive < 5) {
      this.refs.swiper.scrollBy(1);
    }
  }

  render() {
    return (

      <View style={styles.container}>


        <Swiper
          ... etc.
          onIndexChanged={idxActive => this.setState({idxActive})}
        >
          ... etc.

关于react-native-swiper 下一个/上一个按钮事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57303535/

相关文章:

reactjs - 仅在特定屏幕上显示广告(React Native 和导航)

angular - Ion-slides 不更新动态数据

select - swiper插件导致选择无法弹出下拉框

react-native - 如何在 TensorflowJs 中将张量解码为 Base64?

react-native - 如何使用动画在 React Native 中制作循环图像背景

javascript - 如何在 React 中应用外部 SetState

slider - Swiper 触摸事件 - 启用点击但禁用拖动

slider - 滑动 slider 空白

javascript - 我如何最初将幻灯片容器设置为距左边距 10%,并在到达末尾时距右边距 10%? (vue-awesome-swiper)

firebase - React native iOS 应用程序在导入 firebase crashlytics 后卡在启动屏幕上?