简体   繁体   English

React Navigation w/ TypeScript:如何正确输入屏幕参数? 类型 object.ts(2339) 上不存在属性 ID

[英]React Navigation w/ TypeScript: How to properly type out screen params? Property id does not exist on type object.ts(2339)

I'm not wrapping my head around how the types work for screen params.我并没有纠结于屏幕参数的类型是如何工作的。 I looked at the documentation ( https://reactnavigation.org/docs/typescript/ ) but I have no idea where they got the user.id from.我查看了文档( https://reactnavigation.org/docs/typescript/ ),但我不知道他们从哪里得到 user.id。

Here is what is going on with my application.这是我的应用程序发生的事情。 I'm sending the id of the chat room when a user clicks on a chat.当用户点击聊天时,我正在发送聊天室的 ID。

types.tsx - This is where I added my ChatRoom: { id: string } types.tsx - 这是我添加聊天室的地方:{ id: string }

import { BottomTabScreenProps } from '@react-navigation/bottom-tabs';
import {
  CompositeScreenProps,
  NavigatorScreenParams,
} from '@react-navigation/native';
import { NativeStackScreenProps } from '@react-navigation/native-stack';

declare global {
  namespace ReactNavigation {
    interface RootParamList extends RootStackParamList {}
  }
}

export type RootStackParamList = {
  Home: NavigatorScreenParams<RootTabParamList> | undefined;
  Modal: undefined;
  NotFound: undefined;
  ChatRoom: { id: string };
};

export type RootStackScreenProps<Screen extends keyof RootStackParamList> =
  NativeStackScreenProps<RootStackParamList, Screen>;

export type RootTabParamList = {
  TabOne: undefined;
  TabTwo: undefined;
};

export type RootTabScreenProps<Screen extends keyof RootTabParamList> =
  CompositeScreenProps<
    BottomTabScreenProps<RootTabParamList, Screen>,
    NativeStackScreenProps<RootStackParamList>
  >;

ChatRoomItem.tsx - This is where I add the Pressable to navigate to 'ChatRoom' with the params of { id: roomId } ChatRoomItem.tsx - 这是我添加 Pressable 以使用 { id: roomId } 的参数导航到“ChatRoom”的地方

interface Props {
  roomId: string;
  uri: string;
  newMessages?: number;
  name: string;
  createdAt: string;
  lastMessage: string;
}

const ChatRoomItem: React.FC<Props> = ({
  roomId,
  uri,
  newMessages,
  name,
  createdAt,
  lastMessage,
}) => {
  const navigation = useNavigation();

  const openChat = () => {
    navigation.navigate('ChatRoom', {
      id: roomId,
    });
  };

ChatRoomScreen.tsx - This is where the I am getting the error ChatRoomScreen.tsx - 这是我得到错误的地方

const ChatRoomScreen = () => {
  const route = useRoute();

  // Property 'id' does not exist on type 'object'.ts(2339)
  const roomId = route.params?.id; 

  // Logs correct roomId
  console.log(roomId); 

  return (

What is the proper way of fixing this error?修复此错误的正确方法是什么? Thanks for your help.谢谢你的帮助。 First time using TypeScript with a React Native project so I want to understand these param types.第一次在 React Native 项目中使用 TypeScript,所以我想了解这些参数类型。

You have to declare the type of your param in order to use them with useRoute您必须声明参数的类型才能将它们与 useRoute 一起使用

type ParamList = {
  Params: {id:string};
};

const { params } = useRoute<RouteProp<ParamList, 'Params'>>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型'A'.ts(2339)上不存在属性'ref' - React,TypeScript - Property 'ref' does not exist on type 'A'.ts(2339) - React, TypeScript Typescript 通用:“T”类型上不存在属性“pass”.ts(2339) - Typescript generic : Property 'pass' does not exist on type 'T'.ts(2339) Typescript 错误:属性“status”在类型“never”上不存在。ts(2339) - Typescript error: Property 'status' does not exist on type 'never'.ts(2339) "<i>TypeScript: Property &#39;data&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript:类型&#39;{ children?:ReactNode;上不存在属性&#39;data&#39;<\/b> <i>}&#39;.<\/i> }&#39;。<\/b> <i>ts(2339)<\/i> ts(2339)<\/b>" - TypeScript: Property 'data' does not exist on type '{ children?: ReactNode; }'. ts(2339) 如何在Typescript中为其子项设置功能道具类型? TS2339类型上不存在“孩子”属性 - How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339 如何解决打字稿错误 TS2339:“IntrinsicAttributes &amp; …”类型上不存在“XXX”属性? - How to resolve typescript error TS2339: Property 'XXX' does not exist on type 'IntrinsicAttributes & …? 打字稿错误TS2339:'Window'类型中不存在属性'webkitURL' - Typescript error TS2339: Property 'webkitURL' does not exist on type 'Window' TypeScript:错误TS2339:类型“ Window”上不存在属性“ CustomEvent” - TypeScript: error TS2339: Property 'CustomEvent' does not exist on type 'Window' TypeScript 错误:属性“scrollIntoView”在类型“never”上不存在。 TS2339 - TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339 React / Typescript:类型“ object”上不存在属性“ id” - React/Typescript: Property 'id' does not exist on type 'object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM