【问题标题】:how could i pass the key prop to a function?我如何将密钥道具传递给函数?
【发布时间】:2022-01-22 05:40:11
【问题描述】:

我有一个无法解决的关键道具错误。我想我应该通过 key 道具,但它不起作用。这是我的地图功能

<TableRow key={row.id} sx={{ '&:last-child td, &:last-child th': { border: 0 } }}>
  <TableCell component="th" scope="row">
    <Person lastname={row.lastname} firstname={row.firstname} />
  </TableCell>
  {CalculateHours().map((c) => ( DisplayDate(c, {row}) ))}
</TableRow>

这是我的功能:

const DisplayDate = (c, {row}) => {

if (ParsingTime(row.startDate) > c && ParsingTime(row.startDate) < c+1) {
    return <TableCell key={c} style={cellStyle}><hr style={hrStyle.hr1} width={calculatedWidth(ParsingTime(row.startDate), true)}/></TableCell>
} else if(ParsingTime(row.endDate) > c && ParsingTime(row.endDate) < c+1) {
    return <TableCell key={c} style={cellStyle}><hr style={hrStyle.hr2} width={calculatedWidth(ParsingTime(row.endDate), false)} /></TableCell>
} else if(ParsingTime(row.startDate) > c || ParsingTime(row.endDate) < c+1) {
    return <TableCell key={c} style={cellStyle}><hr style={hrStyle.hr3} /></TableCell>
} else {
    return <TableCell key={c} style={cellStyle}><hr style={hrStyle.hr1} /></TableCell>
}

};

如何传递密钥?谢谢解答

【问题讨论】:

标签: javascript reactjs function key styled-components


【解决方案1】:

react 文档说键应该在兄弟对象中 -> https://reactjs.org/docs/lists-and-keys.html#:~:text=Example%3A%20Incorrect%20Key%20Usage

所以对于你的代码,在做地图的时候应该在 DisplayDate 组件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 2022-01-13
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    相关资源
    最近更新 更多