function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bryan Leaman 6Bryan Leaman 6 

lwc Need to refresh child component

I've been unable to force a refresh of a child component in lwc.
Basically, I have something like:
<template>
   <c-child-lwc-component userid={selectedUserId}></c-child-lwc-component>

  <lightning-button  label="Do Something" 
	onclick={handleDoSomething}></lightning-button>
</template>
When the button is hit, I'm calling Apex to do something with the user id and when it's done, the childLwcComponent, if it re-renders properly, will show different results. But after calling the apex function, I cannot get the child component to re-render fully.
 
@track selectedUserId;
import invokeMyApexMethod from '@salesforce/apex'MyClass.myApexMethod';

handleDoSomething() {
		invokeMyApexMethod({
			userId: this.selectedUserId
		})
		.then(result => {
			this.trackedfield = result;
			this.trackederror = undefined;
// Need to re-load child component here!
		}).catch(error => {
			console.log(error);
			this.trackederror = error;
			this.trackedvalue = undefined;
		});
}
I have tried using refreshApex referencing this.selectedUserId. I've  also tried adding a dummy, tracked parameter to my child lwc component so that I can change that value every time my apex method has completed. but it still will not re-load the child component.
 
PriyaPriya (Salesforce Developers) 

Hi Bryan,

Kindly refer this similar question :- 
https://salesforce.stackexchange.com/questions/321785/refresh-child-components-from-parent-lwc

Thanks & Regards,

Priya Ranjan

Vinay MVinay M

Hi Bryan,

  Are you calling the Apex method in parent component ? If so, why not call it in the child component itself. You can create in a method in child component (similar to handleDoSomething which is on parent right now). On parent, when "Do Something" is clicked, the method on child can be called, so you can get result from apex everytime the button is clicked. To call method on child component from parent following syntax can be used :

     

this.template.querySelector('c-child-lwc-component').methodFromChild();


Hope this will help. If not, let me know if I missed something which is why you must  have apex on parent for sure, and display results on child.

 

Thank you,

Vinay.