question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 screenshot from 2018-02-23 10 44 44 React 16

screenshot from 2018-02-23 10 46 44 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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
jcald1commented, Oct 18, 2018

event.currentTarget.getAttribute(‘value’) also seems to work.

9reactions
jeansaignecommented, Oct 29, 2018

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 }) => {...}

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found