如何在调用函数时添加参数

How to add the parameter when calling the function?

本文关键字:添加 参数 函数 调用      更新时间:2023-09-26

我正试图找出如何调用一个函数有一个参数,我使用箭头函数。

举个例子:

_openAddDealer = (dealerInfo) => {
  console.log(dealerInfo);
}

我需要从按钮

中调用它
<FloatingActionButton onClick={this._openAddDealer}>

对于其余的函数,我不需要任何绑定到this或类似的东西。那么,我能做些什么呢?

<FloatingActionButton onClick={this._openAddDealer(dealerInfo)}>

我试过了,但是这个函数在应用加载时被调用,而不是在按钮被按下时。

我假设这是JSX。很可能你要找的是:

<FloatingActionButton onClick={() => this._openAddDealer(dealerInfo)}>

可以这么简单吗:

button.onClick = function() {
  // with button's this
  _openAddDealer.call(this, dealerInfo);
  // with outside this
  _openAddDealer(dealerInfo);
};

?我不知道你在用<等做什么,所以我可能会误解…