【问题标题】: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} 我想打印从函数返回的值。请告诉我该怎么做??

【问题讨论】:

  • 你能不能也提供你的功能,你用的是什么类型的组件?
  • {fn(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>
      

      【讨论】:

        猜你喜欢
        • 2020-01-29
        • 2015-12-01
        • 2018-08-28
        • 2017-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多