2

Consider the following code in React:

// rendered:
<input type="file" onChange={onFileSelected} />

// Event handler:
function onFileSelected(event: ChangeEvent<HTMLInputElement>) {
  console.log('event:');
  console.log(event);
  console.log('event.currentTarget.files:');
  console.log(event.currentTarget.files);
}

Now after selecting a file on my onFileSelected function is triggered, this is the output: enter image description here I marked my confusion in the screenshot: event.currentTarget is null, but event.currentTarget.files is accessible? How is that possible?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

1 Answers1

1

Taken from the Event.currentTarget documentation

Note: The value of event.currentTarget is only available while the event is being handled. If you console.log() the event object, storing it in a variable, and then look for the currentTarget key in the console, its value will be null. Instead, you can either directly console.log(event.currentTarget) to be able to view it in the console or use the debugger statement, which will pause the execution of your code thus showing you the value of event.currentTarget.

Daniel Sindrestean
  • 1,153
  • 6
  • 13