【React Native】路由跳转

  • 跳转的路径,就在href里写/details

  • 路径都是相对于app目录来写的,也就是说app目录就是/

  • 很多时候,需要跳转的组件比较复杂。比方说,要在里面要嵌套按钮,或者其他东西。这种情况下,就可以在Link组件里加个asChild

    import { Link } from "expo-router";
    import { StyleSheet, Text, TouchableOpacity, View } from "react-native";

    export default function Index() {
    return (
    <View style={styles.container}>
    <Text style={styles.title}>这里是首页</Text>
    <Link href="/details" style={styles.link}>
    跳转到详情页(Link)
    </Link>
    <Link href="/details" asChild>
    <TouchableOpacity>
    <Text style={styles.buttonText}>跳转到详情页(Link + asChild)</Text>
    </TouchableOpacity>
    </Link>
    </View>
    );
    }

    const styles = StyleSheet.create({
    container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
    },
    title: {
    fontSize: 40,
    fontWeight: "bold",
    color: "#e29447",
    },
    link: {
    marginTop: 20,
    fontSize: 20,
    color: "#1f99b0",
    },
    buttonText: {
    marginTop: 20,
    fontSize: 20,
    color: "#ff7f6f",
    },
    });

    import { Link } from "expo-router";
    import { StyleSheet, Text, View } from "react-native";

    export default function Details() {
    return (
    <View style={styles.container}>
    <Text style={styles.title}>这里是详情页</Text>

    复制代码
        <Link href="../" style={styles.backText}>
          返回
        </Link>
      </View>
    );

    }

    const styles = StyleSheet.create({
    container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
    },
    title: {
    fontSize: 40,
    fontWeight: "bold",
    color: "#4f9df7",
    },
    backText: {
    marginTop: 20,
    fontSize: 20,
    color: "#67c1b5",
    },
    });

router

方法 作用 说明
router.navigate 跳转到指定页面,最常用。 如果目标页面已在Stack中,直接跳转到现有实例,否则新增页面到Stack。
router.replace 替换掉Stack中所有页面。 因为Stack里之前的页面都被替换了,无法返回上一页。
router.push 强制新增页面到Stack。 无论目标页面是否存在,始终在Stack里新增一个页面。
router.dismiss 关闭当前的页面,返回Stack里的上一个页面。
router.dismissAll 关闭所有中间页面,返回Stack根页面。

此外还有一些不太常用的方法:

  • router.dismiss(2),可以在dismiss里加数字,表示返回几层。

  • router.dismissTo('/'),表示从当前页返回到目标页面,并关闭中间页面。

    import { useRouter } from "expo-router";
    import { StyleSheet, Text, TouchableOpacity, View } from "react-native";

    export default function Index() {
    const router = useRouter();

    复制代码
    return (
      <View style={styles.container}>
        <Text style={styles.title}>这里是首页</Text>
    
        <TouchableOpacity onPress={() => router.navigate('/details')}>
          <Text style={styles.buttonText}>
            跳转(navigate )
          </Text>
        </TouchableOpacity>
        <TouchableOpacity onPress={() => router.replace('/details')}>
          <Text style={styles.buttonText}>
            替换(replace)
          </Text>
        </TouchableOpacity>
      </View>
    );

    }

    const styles = StyleSheet.create({
    container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
    },
    title: {
    fontSize: 40,
    fontWeight: "bold",
    color: "#e29447",
    },
    buttonText: {
    marginTop: 20,
    fontSize: 25,
    color: "#ff7f6f",
    },
    });

相关推荐
用户新1 天前
V8引擎 精品漫游指南--Ignition篇(下 一) 动态执行前的事情
前端·javascript
神探小白牙1 天前
eCharts 多系列柱状图增加背景图
javascript·ecmascript·echarts
Wect1 天前
React 性能优化精讲
前端·react.js·性能优化
薛定猫AI2 天前
【深度解析】Gemma Chat 本地 AI 编程 Agent:Electron + MLX + 开源模型的离线 Vibe Coding 实战
javascript·人工智能·electron
全栈前端老曹2 天前
【前端地图】多地图平台适配方案——高德、百度、腾讯、Google Maps SDK 差异对比、封装统一地图接口
前端·javascript·百度·dubbo·wgs84·gcj-02·bd09
笑虾2 天前
Win10 修改注册表 让鼠标悬停PNG上时 tip 始终显示分辨率
开发语言·javascript·ecmascript
雾岛听风6912 天前
JavaScript基础语法速查手册
开发语言·前端·javascript
用户2367829801682 天前
从零实现 GIF 制作工具:LZW 压缩与 Median Cut 色彩量化
前端·javascript
棉猴2 天前
Python海龟绘图之绘制文本
javascript·python·html·write·turtle·海龟绘图·输出文本
Highcharts.js2 天前
线形比赛积分增长或竞赛图|Highcharts企业图表代码示列
开发语言·前端·javascript·折线图·highcharts·竞赛图