3

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?

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>
    );
  }
}
1
  • 1
    Btw, super(props) is unnecessary unless you need access to props inside of the constructor. Just in case you don't know. Commented Sep 10, 2016 at 9:11

1 Answer 1

3

how would I as well pass the event to the function 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.

So here:

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

it would be the first argument to handleDown, and here:

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

it would be the second argument to handleDown.

I'd probably modify the first bind call to pass null or something so that you consistently get it as the second argument. But unless React does something special, it will always be the last argument, so you could work from that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.