简体   繁体   English

在事件处理程序回调中传递事件+参数

[英]Passing event + arguments in an event handler callback

I can successfully pass an argument from an event handler with the code below. 我可以使用下面的代码成功地从事件处理程序传递参数。

But I wonder, how would I as well pass the event to the function handleDown ? 但我想知道,我如何将event传递给函数handleDown

class Parent extends React.Component {
    handleDown(argumentOne) {
    }
  render () {
    return (
      <div>
        <Child 
            handleDown={this.handleDown.bind(this)}
        />
      </div>
    );
  }
}

class Child extends React.Component {
    constructor(props) {
    super(props);
    }
  render () {
    return (
      <div>
        <span
            onMouseDown={this.props.handleDown.bind(this, 'argumentOne')}
        >
        Click here
        </span>
      </div>
    );
  }
}

how would I as well pass the event to the function handleDown? 我如何将事件传递给函数handleDown?

You don't, the event mechanism does, but given the code in the question, handleDown will receive the event as the first argument after the ones you've provided. 没有,事件机制确实如此,但考虑到问题中的代码, handleDown将在你提供的事件之后接收事件作为第一个参数。

So here: 所以在这里:

<Child 
    handleDown={this.handleDown.bind(this)}
/>

it would be the first argument to handleDown , and here: 它将是handleDown的第一个参数,在这里:

<span
    onMouseDown={this.props.handleDown.bind(this, 'argumentOne')}
>

it would be the second argument to handleDown . 这将是handleDown的第二个参数。

I'd probably modify the first bind call to pass null or something so that you consistently get it as the second argument. 我可能会修改第一个bind调用以传递null或其他东西,以便您始终将其作为第二个参数。 But unless React does something special, it will always be the last argument, so you could work from that. 但除非React做了一些特别的事情,否则它将永远是最后一个参数,所以你可以从中做到。

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

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