为什么里面的函数没有运行

时间:2017-12-19 16:13:14

标签: reactjs function html-table

我想像下面那样呈现表数据。

enter image description here

然而,第二行"可用频道"返回值是数字,我想得到字符串。喜欢0 - 无,1 - 网络,2 - 位......

我尝试使用paymentType函数来切换它,但它不起作用。 似乎这个功能根本没有运行。但我不知道为什么

<table>
 ...
 <tr>
  <td> { this.paymentType(id) } </td>
 <tr>
</table>

我写的功能喜欢:

paymentType(id){
if(id===0) { return "None"}
else if(id===1) { return "Web"}
else if(id===2) { return "Pos"}
}

我只有一个空白。

我将其更改为箭头功能,仍为空行。

 <table>
 ...
 <tr>
  <td> { () => this.paymentType(id) } </td>
 <tr>
</table>

我在构造函数方法中绑定它,仍然是一个空行。

constructor(props){
  super(props)
  this.paymentType = this.paymentType.bind(this)
}

enter image description here

我想知道如何解决这个问题,所有的回复都会受到赞赏。

1 个答案:

答案 0 :(得分:2)

直接调用 paymentType 函数,不要将它包装成箭头函数声明:

{ this.paymentType(payment.payment_type_id) }

正如你所拥有的,你在大括号中有效地使用了函数声明,你没有调用你的辅助函数。