Ben Walker Ben Walker - 1 month ago 8
React JSX Question

react onselect passing value in addition to the Select option values

I'm trying to pass a value into an onselect. If I don't pass a parameter, I can access the values of the option selected just fine, like this:

onSelect={this.onItemSelect()}

onItemSelect = (option) => {
console.log(option);
}


At that point,
option
will have a key and a display value. But I want to also pass in a parameter when calling onItemSelect. When I do that, my option values disappear. Here's what I've tried:

onSelect={this.onItemSelect(myId)}

onItemSelect = (myId, option) => {
console.log(myId);
console.log(option);
}


At that point, myId is fine, but option is undefined. How do I pass both?

Answer Source

You are probably looking for something like this:

onSelect={(option) => this.onItemSelect(myId, option)}