Unable to send @api Object variable or array in LWC from parent to Child

javascriptlightning-web-componentsparent-to-child

Please help whenever I am trying to pass the object then it is not getting passed from Parent to Child but if I am passing string values then it is getting passed to child component.
Please help.

Inside Parent component I am calling child and passing getchecboxDataforChild which returns the object values:

Parent LWC:

<c-generic-checklist 
  chkbox-obj-data={getchecboxDataforChild}
  source-category="inc-source"
  oncheckboxvaluechange={handleChildEvent}
></c-generic-checklist>
handleChildEvent(event) {          
  this.getchecboxDataforChild();   
}
getchecboxDataforChild() {
  console.log('chkdata= ', Object.keys(this.chkboxObjDatafrmParent));

  this.chkboxObjDatafrmParent = {
    headervar:this.labels.somelabel
  }; 

  this.template.querySelector('c-generic-checklist').chkboxObjData = this.chkboxObjDatafrmParent;
  return this.chkboxObjDatafrmParent;
}

Child LWC:

@api chkboxObjData;
@api sourceCategory;

renderedCallback() {
  // value here is blank for some reason
  console.log('chkboxObjData: ', this.chkboxObjData)

  // value here is "inc-source"
  console.log('sourceCategory: ', this.sourceCategory)
}

Best Answer

instead of passing a method getchecboxDataforChild pass the property chkboxObjDatafrmParent

Related Topic