我正在尝试从材料UI渲染一个表格,其中包含一些我提取的数据。我知道我的数据可以正确获取,因为我可以从其保存状态进行console.log记录,但是当我尝试在表行中映射它时,它不起作用。我的表成功生成了其标题,只是正文的空白数据。
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>
);
}
}
答案 0 :(得分:0)
在箭头功能中,如果要编写块{..}
,则必须使用显式的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>
);
});
};