According to the documentation on event dispatching event types should follow these rules:
- No uppercase letters
- No spaces
- Use underscores to separate words
When I attempt to use underscores in my event type I don't seem to be able to handle it.
Dispatcher:
<lightning-button label="Update" onclick={toggleEditAsset}></lightning-button>
toggleEditAsset = () => {
const event = new CustomEvent('toggle_edit_asset');
this.dispatchEvent(event);
}
Handler:
<c-asset-information asset={model.asset} edit-asset={editAsset} ontoggleeditasset={handleToggleEditAsset}></c-asset-information>
handleToggleEditAsset() {
this.editAsset = !this.editAsset
console.log('toggle');
}
This handler does not get called.
If I try to change ontoggleeditasset
to ontoggle_edit_asset
I get an error
LWC1056: Invalid event name ontoggle_edit_asset. Event name can only contain lower-case alphabetic characterslwc
If I change the event to toggleeditasset
everything works fine
What is the proper markup to handle an event with underscores?
Use underscores to separate words
. I'm aware I can do that, but I was more curious about how to do it the way the documentation says to do it