You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何替换React Native App组件中Props的any类型?

替换React Native组件中Props的any类型

你可以通过定义明确的TypeScript类型来替换props: any,具体步骤如下:

1. 定义单个学生的类型

先为学生对象创建类型,匹配给出的props结构:

type Student = {
  name: string;
  course: string;
};

2. 定义组件的Props类型

基于学生类型,定义App组件接收的props类型:

type AppProps = {
  studentList: Student[]; // 注意:你的props示例里写的是`studentlist`(小写l),但代码里用的是`studentList`(大写L),请保持命名一致
};

3. 修改App组件的props类型

把原来的any替换成刚定义的AppProps,同时优化日志输出避免格式异常:

import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';

const Stack = createStackNavigator();

type Student = {
  name: string;
  course: string;
};

type AppProps = {
  studentList: Student[];
};

export default function App(props: AppProps): JSX.Element {
  console.log('props:', props.studentList); // 用逗号拼接避免数组转字符串显示[object Object]
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="React Native">
        <Stack.Screen
          name="React Native"
          component={HomeScreen}
          initialParams={{ studentList: props.studentList }}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

内容的提问来源于stack exchange,提问作者JarochoEngineer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 02:34:54