react native使用TS实现路由

一、入口文件app.jsx的配置

入口文件最好还是要保留jsx

复制代码
import { NavigationContainer } from '@react-navigation/native';
import { navigationRef } from "./src/views/RootNavigation";

const App = () => {
    return(
        <NavigationContainer ref={navigationRef}>
           <StackRouter />
        </NavigationContainer>
    )
}

二、HomeScreen自定义你的tabbar导航

一般情况下项目里都是自定义的tababr,每个项目不一样,我这里就不过多展示

三、在应用的任何地方使用的导航功能

复制代码
import { createNavigationContainerRef } from "@react-navigation/native";
export const navigationRef = createNavigationContainerRef();
export function navigate(name, params) {
    if (navigationRef.isReady()) {
        navigationRef.navigate(name, params);
    }
}

四、配置router.tsx和types.tsx

router.tsx

首先tababr页面可以用const的方式引入,其他页面可以用lazy的方式懒加载引入

核心代码

复制代码
import { createNativeStackNavigator } from "@react-navigation/native-stack";

const Stack = createNativeStackNavigator<RootStackParamList>();

export default function StackRouter() {
  return (
   //示例
    <Stack.Navigator
      initialRouteName="HomeScreen"
      screenOptions={{ headerShown: false }}>
      <Stack.Screen name="HomeScreen" component={HomeScreen} />
    </Stack.Navigator>
)}

types.tsx

核心代码

复制代码
import { StackNavigationProp } from '@react-navigation/stack';
import { RouteProp } from '@react-navigation/native';

export type RootStackParamList = {
    HomeScreen: undefined;
    Search: undefined;
    NpcMessage: undefined;
    Interactive: undefined;
    Example: { data: Array<string> }; //示例
    // 新添加的路由及其参数也应该在这里定义
};

//需要用参数的时候需要导出一个 RouteProp
//需要用到页面跳转的时候,需要导出一个StackNavigationProp
//如果两者都需要,就需要导出两个
//import { useNavigation ,useRoute } from '@react-navigation/native'; 目标页面useNavigation是导航,useRoute是route

//定义Interactive页面路由跳转
export type InteractiveNavigationProp = StackNavigationProp<RootStackParamList, 'Interactive'>;
export type InteractiveRouteProp = StackNavigationProp<RootStackParamList, 'Interactive'>;
// 定义Example页面的路由参数类型
export type ExampleRouteProp = RouteProp<RootStackParamList, 'Example'>;
// 更多类型定义...

五、页面中使用

复制代码
import { useNavigation, useRoute } from '@react-navigation/native';
import { InteractiveNavigationProp,InteractiveRouteProp } from '../../router/types'


const Interactive = () => {
    
    const navigation = useNavigation<InteractiveNavigationProp>();
    const router = useRoute<InteractiveRouteProp>


}

export default Interactive;

原本我们项目是js,由于最近在学TS,就在项目上慢慢局部引入TS,如果有问题或者建议,欢迎提问。

相关推荐
程序员Sunday6 小时前
TypeScript 的 any、unknown、never 有什么区别?
前端·面试·typescript
三十而立洋12 小时前
TypeScript 7.0 深度解析:新特性、Go 原生编译器及与 TypeScript 6.0 的区别
前端·typescript·前端工程化
VimorphTech12 小时前
React 自动保存怎么设计?防抖、串行请求与导出前等待
后端·typescript
墨狂之逸才13 小时前
# React Native 0.59 在 Node 24 下的启动兼容问题:从报错到一条命令运行
react native
yuanlaile16 小时前
做了 3 年后端才明白:学 NestJS 千万不要只学 API 语法,完整电商项目才是分水岭
typescript·nestjs·node.js 后端·nestjs实战
ZoeLandia17 小时前
TypeScript 07:工程化——import type、声明文件与 tsconfig
开发语言·前端·typescript
nyaomaru19 小时前
如何用 Jev 从 PR 中提取 “Why” 并写入 CHANGELOG?🤔
前端·typescript
天道kabuto19 小时前
入门 React Native:初始化 HelloRN 项目,Android 环境排错全记录
react native
damoluomu19 小时前
SSG 的后台怎么补:Node.js 生态里的三条路(番外二)
javascript·css·typescript·node.js·html
苏渡苇21 小时前
从零搭建 Vue 3 工程化项目:Vite + TypeScript + Pinia + Element Plus 实战
前端·vue.js·typescript