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">
         
相关推荐
正小安1 分钟前
优秀博客:小程序通信方法在 Vue 3 中的对应技术
前端·vue.js·小程序
小彭努力中18 分钟前
52. OrbitControls辅助设置相机参数
前端·3d·webgl
Axlxhxx18 分钟前
vue 流式加载mp4文件
开发语言·前端·javascript
黄尚圈圈23 分钟前
Maven超详细教程(三):Maven依赖查找顺序
java·前端·maven
没那么特别的特别32 分钟前
Vue+NestJS项目实操(图书管理后台)
前端·javascript·vue.js
如果'\'真能转义说1 小时前
Ajax ( 是什么、URL、axios、HTTP、快速收集表单 )Day01
前端·javascript·ajax
elirlove11 小时前
CSS在线格式化、美化、压缩工具
前端·css
码农幻想梦1 小时前
html5 + css3(下)
前端·css3·html5
与妖为邻1 小时前
房屋水电费:重新布局,重构JS代码
前端·javascript·css·房租水电费记账本
塔塔开!.1 小时前
HTML中的padding和margin
前端·html