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">
         
相关推荐
无限大622 分钟前
《AI观,观AI》:善用AI赋能|让AI成为你深耕核心、推进重心的“最强助手”
前端·后端
烛阴41 分钟前
Claude Code Skill 从入门到自定义完整教程(Windows 版)
前端·ai编程·claude
lxh01131 小时前
数据流的中位数
开发语言·前端·javascript
神仙别闹1 小时前
基于NodeJS+Vue+MySQL实现一个在线编程笔试平台
前端·vue.js·mysql
zadyd2 小时前
Workflow or ReAct ?
前端·react.js·前端框架
北寻北爱4 小时前
vue2和vue3使用less和scss
前端·less·scss
IT_陈寒4 小时前
Redis性能提升3倍的5个冷门技巧,90%开发者都不知道!
前端·人工智能·后端
雨雨雨雨雨别下啦4 小时前
Vue案例——面经
前端·javascript·vue.js
oo121385 小时前
里程碑5 - 完成框架 npm 包抽象封装并发布
前端·npm
达拉5 小时前
我花了三天用AI写了个上一代前端构建工具
前端·前端工程化