简体   繁体   English

反应本机中的循环组件

[英]Loop component in react native

I want to return a components like table using the following object.我想使用以下 object 返回一个类似表格的组件。 But I must use only points array.但我必须只使用点数组。 Do not need to head tab.不需要头部标签。

const states = {
  player: [
    {
      id: 1,
      name: 'David',
      points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2]
    },
    {
      id: 2,
      name: 'John',
      points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2]
    },
    {
      id: 3,
      name: 'Sam',
      points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2]
    },
    {
      id: 4,
      name: 'Johanson',
      points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2]
    }
  ]
} 

I am using DataTable in react-native-paper.我在 react-native-paper 中使用 DataTable。 It has this structure;它具有这种结构;

 <DataTable>
        <DataTable.Header>
          <DataTable.Title>Dessert</DataTable.Title>
          <DataTable.Title>Calories</DataTable.Title>
          <DataTable.Title>Fat</DataTable.Title>
        </DataTable.Header>

        <DataTable.Row>
          <DataTable.Cell>Frozen yogurt</DataTable.Cell>
          <DataTable.Cell>159</DataTable.Cell>
          <DataTable.Cell>6.0</DataTable.Cell>
        </DataTable.Row>

      </DataTable>

I would go about it like this:我会 go 像这样:

Edit: Since you want to render the points in columns instead of rows, I am assuming all players have equal number of points and using first player's points to render the rows.编辑:由于您想在列而不是行中呈现点,我假设所有玩家都有相同数量的点并使用第一个玩家的点来呈现行。

const states = {
  player: [
    {
      id: 1,
      name: "David",
      points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2],
    },
    {
      id: 2,
      name: "John",
      points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2],
    },
    {
      id: 3,
      name: "Sam",
      points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2],
    },
    {
      id: 4,
      name: "Johanson",
      points: [3, 4, 8, 2, 5, 3, 9, 5, -4, -6, 2],
    },
  ],
};

function MyDataTable() {
  return (
    <DataTable>
      {states.player[0].points.map((p, i) => (
        <DataTable.Row key={p}>
          {states.player.map((player) => (
            <DataTable.Cell>{player.points[i]}</DataTable.Cell>
          ))}
        </DataTable.Row>
      ))}
    </DataTable>
  );
}

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

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