【问题标题】:How to print a value inside a map returning from a function [duplicate]如何在从函数返回的地图中打印值[重复]
【发布时间】:2021-07-22 11:51:09
【问题描述】:
这是一个 TableCell,它位于 array.map((row,index) =>{}) 中
<TableCell component="th" scope="row" padding="none">
{row.createdAt}
</TableCell>
我想将此 {row.createdAt} 作为参数传递给函数,而不是在其中打印 {row.createdAt} 我想打印从函数返回的值。请告诉我该怎么做??
【问题讨论】:
标签:
javascript
reactjs
function
【解决方案1】:
您可以在 TableCell 组件内的括号内调用函数
<TableCell component="th" scope="row" padding="none">
{yourFunction(row.createdAt)}
</TableCell>
你可以通过在 {} 中调用 JSX 中的函数来调用它
【解决方案2】:
这很简单,只需调用 function 并传递 row.createdAt
<TableCell component="th" scope="row" padding="none">
{this.yourFunctioName(row.createdAt)} // if class based component
// OR {yourFunctioName(row.createdAt)} if function based component
</TableCell>
【解决方案3】:
只需创建一个函数然后调用它,然后将 row.createdAt 作为参数传递
function getCreatedAt(createdAt) {
// Do something
// HERE
return createdAt
}
<TableCell component="th" scope="row" padding="none">
{getCreatedAt(row.createdAt)}
</TableCell>