5

I am using and I am implementing a dropdown menu in my react web page. In my dropdown menu, I am calling a handleClick function when button is selected.

<Dropdown.Menu>
    <Dropdown.Item name = "baudratestate1200" onSelect={this.handleClick}>1200</Dropdown.Item>
    <Dropdown.Item name = "baudratestate2400" onSelect={this.handleClick}>2400</Dropdown.Item>
    <Dropdown.Item name = "baudratestate4800" onSelect={this.handleClick}>4800</Dropdown.Item>
    <Dropdown.Item name = "baudratestate9600" onSelect={this.handleClick}>9600</Dropdown.Item>
</Dropdown.Menu>

Here is my handleClick function:

handleClick = (eventkey, event) => {
  console.log(eventkey)
  console.log(event)
}

However, the event.target is null, attached below is the console.log from my client browser:

Class {dispatchConfig: {…}, _targetInst: FiberNode, _dispatchInstances: FiberNode, nativeEvent: MouseEvent, _dispatchListeners: ƒ, …}
nativeEvent: (...)
type: (...)
target: null

I have tried binding and not-binding the function in the constructor but both have produced null values for event.target

this.handleClick = this.handleClick.bind(this);

What am I doing wrong here? Any form of concept clarification would be greatly appreciated!

3
  • You need this to get the value of the item, right? Commented Feb 5, 2020 at 15:53
  • @MoshFeu that is right, i need to get the selected value Commented Feb 5, 2020 at 16:26
  • Have you tried onSelect instead of onClick? Commented Feb 5, 2020 at 16:52

1 Answer 1

1

You have some options.

  1. React is throw a warning when trying to access the event

Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the property nativeEvent on a released/nullified synthetic event. This is set to null. If you must keep the original synthetic event around, use event.persist(). See ... for more information.

So, in order to get access to the original event, you should call to event.persist() before you access the event.

Like this:

handleClick = (eventkey, event) => {
  event.persist();
  console.log(eventkey)
  console.log(event)
}
  1. You can use the eventkey to pass the value
change = eventkey => {
  // a.persist();
  alert(`you chosen: ${eventkey}`);
};
<Dropdown onSelect={this.change}>
  <Dropdown.Toggle variant="success" id="dropdown-basic">
    Dropdown Button
  </Dropdown.Toggle>
  <Dropdown.Menu>
    <Dropdown.Item eventKey="baudratestate1200">1200</Dropdown.Item>
    <Dropdown.Item eventKey="baudratestate2400">2400</Dropdown.Item>
    <Dropdown.Item eventKey="baudratestate4800">4800</Dropdown.Item>
    <Dropdown.Item eventKey="baudratestate9600">9600</Dropdown.Item>
  </Dropdown.Menu>
</Dropdown>

https://codesandbox.io/s/react-bootstrap-dropdown-get-value-r7n0f

  1. Call the function with hardcoded param
change = option => {
  alert(`you chosen: ${option}`);
};
<Dropdown.Item onSelect={()=> this.change("baudratestate1200")}> 1200
</Dropdown.Item>
<Dropdown.Item onSelect={()=> this.change("baudratestate2400")}> 2400
</Dropdown.Item>
<Dropdown.Item onSelect={()=> this.change("baudratestate4800")}> 4800
</Dropdown.Item>
<Dropdown.Item onSelect={()=> this.change("baudratestate9600")}> 9600
</Dropdown.Item>

https://codesandbox.io/s/react-bootstrap-dropdown-get-value-6gknv

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

2 Comments

ahh i see, the event.persist() was the issue here, thanks so much for the help!!! passing the value through event key is a neat little trick too!
Sure :) Good luck!

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.