While passing data from Child Component to Parent Component, I can definitely Dispact Custom Event and populate the 'detail' property. However, it seems (like React), I can also pass callback function from the Parent to Child and execute the callback with data on event. Parent.Html
<template>
<c-child call-back={handleNotification}></c-child>
{message}
</template>
Parent.JS
import { LightningElement, track, api } from 'lwc';
export default class App extends LightningElement {
message=''
//Callback
handleNotification=(val)=>{
this.message=val;
}
}
Child.HTML
<template>
<div>
<lightning-button label='Click here' variant='brand' onclick={raiseEvent}></lightning-button>
</div>
</template>
Child.JS
import { LightningElement, track, api } from 'lwc';
export default class Child extends LightningElement {
@api callBack;
raiseEvent=()=>{
console.log('raising Event');
// DISPATCH EVENT or CaLL Callback ??
this.callBack('Deep');
}
}
Which pattern should be used one?
on<eventname>={handlerMethod}
in the parent. It is best practice to keep the event name all lowercase.