【问题标题】:Cannot render material ui table row无法渲染材质ui表格行
【发布时间】:2018-11-27 03:55:12
【问题描述】:

我正在尝试从材质 UI 渲染一个表格,其中包含我获取的一些数据。我知道我的数据正在正确获取,因为我能够从其保存状态对其进行控制台记录,但是当我尝试将其映射到表行中时,它不起作用。我的表格成功生成了它的标题,只是正文有空白数据。

gameDataTableRow = scheduleData => {
    scheduleData.map(game => {
      console.log(game.scheduled); <--These console logs will display data properly
      console.log(game.venue.name);
      console.log(game.home.name);
      console.log(game.away.name);
      return (
        <TableRow key={game.venue.id}>
          <TableCell>{game.scheduled}</TableCell> <-- These cells wont render in the table row
          <TableCell>{game.venue.name}</TableCell>
          <TableCell>{game.home.name}</TableCell>
          <TableCell>{game.away.name}</TableCell>
        </TableRow>
      );
    });
  };

render() {
    console.log(this.state.games);
    return (
      <Table>
        <TableHead>
          <TableRow>
            <TableCell numeric>Date</TableCell>
            <TableCell>Venue</TableCell>
            <TableCell>Home</TableCell>
            <TableCell>Away</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>{this.gameDataTableRow(this.state.games)}</TableBody>
      </Table>
    );
  }
}

【问题讨论】:

    标签: javascript reactjs react-native material-ui


    【解决方案1】:

    在箭头函数中,如果你正在写块{..},你必须使用显式的return语句。

    这两个是一样的:

    let foo1 = (val1) => val1 * 2;
    let foo1 = (val1) => { return val1 * 2 };
    

    问题的解决方案:

    gameDataTableRow = scheduleData => {
       return scheduleData.map(game => { //Added return statement
        console.log(game.scheduled); <--These console logs will display data properly
        console.log(game.venue.name);
        console.log(game.home.name);
        console.log(game.away.name);
        return (
        <TableRow key={game.venue.id}>
          <TableCell>{game.scheduled}</TableCell> <-- These cells wont render in the table row
          <TableCell>{game.venue.name}</TableCell>
          <TableCell>{game.home.name}</TableCell>
          <TableCell>{game.away.name}</TableCell>
        </TableRow>
        );
      });
    };
    

    【讨论】:

    • 天哪,谢谢!我已经坚持了这么久。谢谢!
    • 很高兴收到您的来信。快乐编码。 :-)
    猜你喜欢
    • 2019-06-02
    • 2021-04-18
    • 2019-10-05
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多