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.
refer : Event System in React
Events are defined within event pools . Each activity has an event pool, which contains the set of events that it recognizes (that is, events that have been defined to it, and system events). An activity's event pool is initialized when the activity is created, and deleted when the activity is deleted.
Overview. Your event handlers will be passed instances of SyntheticEvent , a cross-browser wrapper around the browser's native event. It has the same interface as the browser's native event, including stopPropagation() and preventDefault() , except the events work identically across all browsers.
In React Application:The onClick is an event handler for the target element within our React application. Generally, this event handler specifies which function must be executed after that particular element is called. We add the onClick event handler to the elements as the attributes.
An event is an action that could be triggered as a result of the user action or system generated event. For example, a mouse click, loading of a web page, pressing a key, window resizes, and other interactions are called events.
Event Pooling - React uses SyntheticEvent which is a wrapper for native browser events so that they have consistent properties across different browsers. The event handlers that we have in any react-app are actually passed instances of SyntheticEvent unless we use nativeEvent attribute to get the underlying browser event.
Wrapping native event instances can cause performance issues since every synthetic event wrapper that's created will also need to be garbage collected at some point, which can be expensive in terms of CPU time.
React deals with this problem by allocating a synthetic instance pool. Whenever an event is triggered, it takes an instance from the pool and populates its properties and reuses it. When the event handler has finished running, all properties will be nullified and the synthetic event instance is released back into the pool. Hence, increasing the performance.
It means that the properties of the event only exist while the callback is active. Adding async to the mix, or storing the event for future use, will fail.
This is easily observed if you try console.log(event)
inside an event handler. By the time you inspect the object, most properties on the event object will be null
. If you stop execution of the script with debugger;
immediately after logging the value, you can inspect the values.
class MyComponent extends React.Component { handleClick (e){ console.log('The event currentTarget is', e.currentTarget); // DOM element setTimeout(() => { console.log('event.currentTarget was', e.currentTarget); // null }, 1000) } render () { return <button onClick={this.handleClick}>Fire event!</button> } }
This will log a DOM element when you click the button, and null
a second later. For reasons beyond me, event.target
is still stored until the next event occurs, and not nullified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With