I am trying to pass some data from a child component to a parent component by following these docs: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.events_create_dispatch
However, for me, the data returned is always NaN. However, the parent component can see that an event is happening.
here are my code blocks!
Parent Component:
@track loaded = false;
initalData = [];
@wire(getConnections) proccessData({ error, data }) {
if (data) {
this.initalData = data;
console.log(data);
this.loaded = true;
} else {
console.log(error);
}
}
onSelected(event) {
console.log('Event from Child', + event.detail);
}
}
<template>
<div class="slds-box slds-theme_default">
<div class="slds-is-relative">
<div if:false={loaded} class="">
<lightning-spinner alternative-text="Loading" size="large" variant="brand"></lightning-spinner>
</div>
<div if:true={loaded}>
<c-frontend_visualpicker connection-data={initalData} onselected={onSelected}></c-frontend_visualpicker>
</div>
</div>
</div>
</template>
Child Component JS:
handleClick(event){
// event.preventDefault()
const eventdata = event.target.dataset.id;
const selectedEvent = new CustomEvent('selected', { detail: eventdata});
this.dispatchEvent(selectedEvent);
console.log('data: ' + eventdata);
}
Also here is how it looks like in dev console in chrome, ideally the event data should appear in both console.logs: