How do you pass data to an event handler while dispatching an event?
-
Use the property called “data” in event Object
-
Use the property called “payload” in event Object
-
Create a Custom Event and have your own public variable to hold the data to be passed to the handler
-
No way to do that.
The standard and most flexible way to pass custom data to an event handler is to create a custom Event class with public properties to hold the data. The base Event object does not have built-in properties like 'data' or 'payload' for arbitrary data. By extending Event and adding your own properties, you can type-safely pass any data needed by the handler.
To pass custom data to an event handler, the standard approach is to create your own custom event class (extending the base event type) with a public property/variable that carries the payload, then read that property inside the handler when the event fires. There's no generic built-in "data" or "payload" property on a plain event object for arbitrary custom data — that flexibility is exactly why custom event subclasses exist.