event.currentTarget.value returns undefined in React 16
See original GitHub issue "react": "^16.2.0",
"react-dom": "^16.2.0"
Just upgraded to react 16 from 15.0.2, and using event currentTarget.value
will give me undefined. Basically I have a stateless child component that will fire the callback when it’s clicked, and will pass an extra params through DOM API value field
<a className="btn-sm" onClick={onDelete} value={data}>
<i className="fa fa-trash" aria-hidden="true" />
</a>
And in my parent component the callback is as follows, the param is not accessible in 16 anymore
onDelete(event) {
const data = event.currentTarget.value; //works in react 15.0.2, but returns undefined in 16
...
}
So I looked into the event that is being returned in both versions of React, looks like in 16 the _dispatchInstances
is a FiberNode
, whereas in 15 is a ReactDOMComponent
React 16
React 15
Is this an intentional change? If so, how do I go about passing this params to the callback? I don’t really want to use arrow functions since that affects the performance
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Having an issue with e.target.value returning Undefined in React
In React, e.target.value will appear null if it's not saved in another variable.
Read more >undefined value from e.target.value : r/reactjs - Reddit
The e.target is attached to the event (click, hover, etc.), which has no value. e.currentTarget is attached to the listener, which ...
Read more >currentTarget Event Property - W3Schools
The currentTarget event property returns the element whose event listeners triggered the event. This is particularly useful during capturing and bubbling. The ...
Read more >event.target.value is undefined for buttons that wrap an image ...
Coding example for the question event.target.value is undefined for buttons that wrap an image instead of text-Reactjs.
Read more >Event Bubbling and Event Catching in JavaScript and React
✨ Event Listeners In React Version 16 and before VS Version 17+; ✨ Special Edge Case: What If You Need an Outer Parent...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
event.currentTarget.getAttribute(‘value’) also seems to work.
Hi there, i just went through the same problem asking myself what was wrong with me. Looking at official React documentation you have to use
event.persist()
on SyntheticEvent in order the target property to be filled correctly; this is because it’s an asynchronous way to handle events and so, its values are only promise before doing “event.persist()”.There is a better way to handle it: use destructuring on the event object. Instead of referencing the event parameter as
(event) => {...}
you can reference its target directly using desctructuring as follow:({ target }) => {...}