简体   繁体   English

React:将函数作为道具传递

[英]React: Passing functions as props

I have a question concerning passing functions as props.我有一个关于将函数作为道具传递的问题。 In the tic-tac-toe tutorial ( https://facebook.github.io/react/tutorial/tutorial.html ) at the end the Game component passes the onClick handler as such:在井字棋教程( https://facebook.github.io/react/tutorial/tutorial.html )最后,游戏组件传递了 onClick 处理程序,如下所示:

<div className="game-board">
  <Board
    squares = { current.squares }
    onClick = {(i) => this.handleClick(i) }
  />
</div>

First, why cant we pass the function like this instead:首先,为什么我们不能像这样传递函数:

onClick = { this.handleClick(i) }

And I understand that passing "i" is important but something in the middle of the tutorial confused me:而且我知道传递“i”很重要,但教程中间的一些内容让我感到困惑:

return <Square value={this.state.squares[i]} onClick={() => this.handleClick(i)} />;

Here we don't pass "i" in the parenthesis of the arrow function.这里我们没有在箭头函数的括号中传递“i”。 I don't want to write too much to make the question less verbose.我不想写太多以使问题不那么冗长。 I am sure some people have gone through this tutorial and will be able to give an answer my questions.我相信有些人已经完成了本教程,并且能够回答我的问题。

UPDATE: Tnx everyone for the brief and helpful answers.更新:感谢大家提供简短而有用的答案。 As a follow-up, in the official docs, we are told to bind a function if it is to be used as an event handler for a component.作为后续,在官方文档中,如果要将函数用作组件的事件处理程序,我们被告知要绑定一个函数。 Why is this necessary and how come it was never used it the tutorial?为什么这是必要的,为什么它从未在教程中使用过?

This doesn't pass the function (it calls the function before binding to onClick ):这不会传递函数(它在绑定到onClick之前调用函数):

onClick = { this.handleClick(i) }

Alternatively, you could bind:或者,您可以绑定:

onClick = { this.handleClick.bind(null, i) }

But the arrow function seems clearer to read (at least IMHO).但是箭头函数似乎更清晰易读(至少恕我直言)。

For the second issue, the i is a parameter for handleClick , not onClick .对于第二个问题, ihandleClick的参数,而不是onClick onClick does have parameters (the first being the event object), but in this case, you don't need anything from the onClick parameters, so they are left empty. onClick确实有参数(第一个是event对象),但在这种情况下,您不需要onClick参数中的任何内容,因此它们留空。

What you put in the onClick parameter will execute during the rendering process, which is not what you want to do.你在onClick参数里放的东西会在渲染过程中执行,这不是你想做的。 You want to execute some actions during a DOM event.您想在 DOM 事件期间执行一些操作。

That's why the tutorial gives you the fat arrow syntax: it means that you are calling a function that returns another function (in this case your handleClick method) so when the markup is rendered, it will execute the function inside the parameter and return your function, that will do the real job when a user clicks on the element.这就是本教程为您提供粗箭头语法的原因:这意味着您正在调用一个返回另一个函数的函数(在本例中为您的 handleClick 方法),因此在呈现标记时,它将执行参数内的函数并返回您的函数,当用户点击元素时,这将完成真正的工作。

As Davin said,正如戴文所说,

onClick = { this.handleClick(i) }

This expression actually calls that function, now then how to pass arguments to the function, see there are 2 ways:这个表达式实际上调用了那个函数,那么现在如何将参数传递给函数,看看有两种方法:

First: In this function, you can use your argument i and this object, which refers to current component.第一:在这个函数中,你可以使用你的参数ithis对象,它指的是当前组件。

(i) => this.handleClick(i)

Second: Here, you can use your argument but cannot use this object of the component, where null will be DOM element on which event is fired.第二:在这里,您可以使用您的参数,但不能使用组件的this对象,其中null将是触发事件的 DOM 元素。

this.handleClick.bind(null, i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM