Is there any way to use refreshApex to refresh data passing from Parent to Child?
I have 2 components: Parent & Child. The parent will perform logic to make the data clean then pass to Child (list of records). The Child will use that public property (@api) to do some update.
Apex Controller
@AuraEnabled public static List<Sobject> getDatas(Boolean isRecentlyView, List<String> fields, String sobjectType, String query, Integer limitNum){ try { if (limitNum != null) { if (isRecentlyView) { query = 'SELECT ' + String.join(fields, ', ') + ' FROM ' + sobjectType + ' WHERE LastViewedDate !=null ORDER BY LastViewedDate DESC '; query += ' LIMIT :limitNum'; } else { query = query.split('FROM')[1]; query = 'SELECT ' + String.join(fields, ', ') + ' FROM ' + query; query += ' LIMIT :limitNum'; } System.debug(query); return Database.query(query); } else return new List<SObject>(); } catch (Exception e) { throw e; } } Parent JS code
import getDatas from '@salesforce/apex/ParentController.getDatas'; import { LightningElement, api, track, wire } from 'lwc'; import {refreshApex} from '@salesforce/apex'; export default class ParentComponent extends LightningElement { datas = []; isRecentlyView = false; sObjectType = 'Account'; numberOfRecords = '10'; getData(){ //do something to populate fields and query getDatas({ isRecentlyView: _this.isRecentlyView, fields: fields, // passing fields here sobjectType: _this.sObjectType, query: query, // passing query here limitNum: _this.numberOfRecords}) .then(result => { _this.datas = result;}) .catch(e => {console.error(e); }) } Child JS code
@api datas; @api objectApiName; groupByValue = 'Type'; recordId = ''; handleListItemDrag(event){ this.recordId = event.detail; } handleItemDrop(event){ let status = event.detail; this.updateHandler(stage); } updateHandler(status){ updateRecord({objectType : this.objectApiName, recordId : this.recordId, fieldApiName: this.groupByValue, newValue: status}) .then((result) => { if (result) const evt = new ShowToastEvent({ message: 'Record updated.', variant: 'success' }); this.dispatchEvent(evt); refreshApex(this.datas); }) .catch((error) => { console.log('Error save',error); }) } Right now, I tried to call refreshApex inside Child component but it has not worked yet (above code)
I also tried to create a custom event and pass to Parent but it does not work too.
Parent HTML code
<template> <c-child-componet datas={datas} object-api-name={objectApiName} onupdaterecord={handleUpdateRecord}> </c-child-component> </template> Parent JS code
handleUpdateRecord(){ refreshApex(this.datas); } Child JS code
updateHandler(status){ updateRecord({ objectType : this.objectApiName, recordId : this.recordId, fieldApiName: this.groupByValue, newValue: status}) .then((result) => { if (result) const evt = new ShowToastEvent({ message: 'Record updated.', variant: 'success'}); this.dispatchEvent(evt); const event = new CustomEvent('updaterecord'); this.dispatchEvent(event); }) .catch((error) => { console.log('Error save',error); }) } Thank you so much