繁体   English   中英

React-Native 中的映射函数

[英]map function in React-Native

我是 React Native 的新手,我正在为我的项目使用 Typescript。 我很难在 React Native 中做映射。 下面是我的接口代码:

interface Teacher {
  name: string;
  sex: string;
  age: number;
  student: Student[];
}

interface Student {
  name: string;
  sex: string;
  address: string;
}

我在映射教师界面时没有任何问题,但是当我在我的代码中使用 map 函数时,我在如何映射学生界面方面遇到了困难。

{Class.map(class => (
   <View>
      {class.student.map(class => (
          <Text>{class.name}</Text>
      ))}
      {class.student.map(class => (
          <Text>{class.sex}</Text>
      ))}
      {class.student.map(class => (
          <Text>{class.address}</Text>
      ))}
   </View>
)}

当我像这样进行编码时,出现错误Cannot read property 'map' of undefined控制台Cannot read property 'map' of undefined 先感谢您。

你可以像这样创建嵌套数组。

interface Teacher {
  name: string;
  sex: string;
  age: number;
  student: Student[{
name: string;
  sex: string;
  address: string;
}];
}

{Teacher.map(({name,sex,address,student}) => (
   <View>
      {student.map(student => (
          <Text>{student.name}</Text>
          <Text>{student.sex}</Text>
          <Text>{student.address}</Text>
      ))}
   </View>
)}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM