The SyntheticEvent
is pooled. This means that the SyntheticEvent
object will be reused and all properties will be nullified after the event callback has been invoked. This is for performance reasons. As such, you cannot access the event in an asynchronous way.
function onClick(event) {
console.log(event);
console.log(event.type);
const eventType = event.type;
setTimeout(function() {
console.log(event.type);
console.log(eventType);
}, 0);
this.setState({clickEvent: event});
this.setState({eventType: event.type});
}
Note:
If you want to access the event properties in an asynchronous way, you should call event.persist()
on the event, which will remove the synthetic event from the pool and allow references to the event to be retained by user code.