taro RN 左右滑动切换页面

引入 react-native-pager-view 组件

javascript 复制代码
import React, { Component } from 'react'
import Taro from '@tarojs/taro'
import { View, PagerView, Button } from '@tarojs/components'
import PagerView from 'react-native-pager-view';
 
export default class MyComponent extends Taro.Component {
  constructor(props) {
    super(props)
    this.pagerViewRef = React.createRef()
    this.state = {
      currentPage: 0,
    }
  }
 
  goToPage = (pageNumber) => {
    if (this.pagerViewRef.current) {
      this.pagerViewRef.current.setPage(pageNumber)
      this.setState({ currentPage: pageNumber })
    }
  }
 
  render() {
    return (
      <View>
        <PagerView
          ref={this.pagerViewRef}
          initialPage={0}
          style={{ height: 200 }}
          onPageSelected={(e) => this.setState({ tabIndex: e.nativeEvent.position })}
        >
          <View style={{ backgroundColor: 'red', flex: 1 }}>第一页</View>
          <View style={{ backgroundColor: 'blue', flex: 1 }}>第二页</View>
          <View style={{ backgroundColor: 'green', flex: 1 }}>第三页</View>
        </PagerView>
        <Button onClick={() => this.goToPage(1)}>Go to Page 1</Button>
      </View>
    )
  }
}

react native

javascript 复制代码
import * as React from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import PagerView from 'react-native-pager-view';
 
const Tabs = () => {
  const [page, setPage] = React.useState(0);
 
  return (
    <View style={styles.container}>
      <View style={styles.tabBar}>
        <TouchableOpacity onPress={() => setPage(0)}>
          <Text>Tab 1</Text>
        </TouchableOpacity>
        <TouchableOpacity onPress={() => setPage(1)}>
          <Text>Tab 2</Text>
        </TouchableOpacity>
        <TouchableOpacity onPress={() => setPage(2)}>
          <Text>Tab 3</Text>
        </TouchableOpacity>
      </View>
      <PagerView style={styles.pagerView} initialPage={page} onPageSelected={e => setPage(e.nativeEvent.position)}>
        <View key="1">
         
相关推荐
风止何安啊3 分钟前
深入 V8 引擎:JavaScript 执行机制与作用域模型的底层逻辑解析
javascript
你算哪颗溜溜梅4 分钟前
uni.scanCode vs MpaasScan:支付宝扫码识别赢麻了,保姆级教程来咯~
javascript·uni-app
JackieDYH5 分钟前
CSS实现跑马灯效果-案例
前端·css·css3
羽沢317 分钟前
Vue3组件间通信——pinia
前端·javascript·vue.js
BBB努力学习程序设计9 分钟前
简易横向导航制作指南
前端·html
BBB努力学习程序设计10 分钟前
深入理解CSS定位叠放次序:z-index完全指南
前端·html
头疼84611 分钟前
vue 组件实现 、background-hover随鼠标丝滑移动~
前端
焦糖小布丁37 分钟前
加http和https访问的网站不同?
前端
人工智能的苟富贵38 分钟前
用 Rust 写一个前端项目辅助工具:JSON 格式化器
前端·rust·json
季春二九1 小时前
Edge 卸载工具 | 版本号1.0 | 专为彻底卸载Microsoft Edge设计
前端·microsoft·edge·edge 卸载工具