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">
         
相关推荐
天宇&嘘月2 小时前
web第三次作业
前端·javascript·css
小王不会写code2 小时前
axios
前端·javascript·axios
发呆的薇薇°3 小时前
vue3 配置@根路径
前端·vue.js
luckyext4 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)4 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
前端没钱4 小时前
前端需要学习 Docker 吗?
前端·学习·docker
前端郭德纲4 小时前
前端自动化部署的极简方案
运维·前端·自动化
海绵宝宝_4 小时前
【HarmonyOS NEXT】获取正式应用签名证书的签名信息
android·前端·华为·harmonyos·鸿蒙·鸿蒙应用开发
码农土豆5 小时前
chrome V3插件开发,调用 chrome.action.setIcon,提示路径找不到
前端·chrome
鱼樱前端5 小时前
深入JavaScript引擎与模块加载机制:从V8原理到模块化实战
前端·javascript