0

In React I have created a button, like this:

<Link size="button" onClick={this._handleShareClick.bind(this, this.props.shareID)} href={this.props.URL}>{shareText}</Link>

What I want to do is pass the share ID, but also have access to the regular event object, to allow me to use e.preventDefault();

When I pass to the handler:

_handleShareClick = (e, shareID) => {
    console.log(shareID);
    e.preventDefault();
}

The shareID doesn't console log, and I also get the following error in my console:

Uncaught TypeError: Cannot read property 'preventDefault' of undefined

Is there a reason I can't access both of these things, or is there another approach that I am missing?

2
  • 3
    You are binding the first parameter to this.props.shareID, so you probably want _handleShareClick = (shareID, e) => .... Commented Nov 3, 2015 at 17:43
  • 1
    Why don't you access this.props.shareID directly in _handleShareClick? Commented Nov 3, 2015 at 19:40

2 Answers 2

2

You should use:

onClick={(e) => (e.preventDefault(), this._handleShareClick(this.props.shareID))
Sign up to request clarification or add additional context in comments.

Comments

0

If u need to pass custom parameters, then u can simply pass the parameters to the bind call. The SyntheticEvent will be passed as the second parameter to the handler.

handleClick(param, e) {
  console.log('Parameter', param);
  console.log('Event', e);
}

render() {
  <button onClick={this.handleClick.bind(this, 'Parameter')}></button>
}

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.