0

In LWC, I am trying to implement onload/doInit logic as in aura component. for a certain scenario I have to reinvoke/call that doInit method from the parent component.

Instead doInt,in LWC am achieving this using @api on connectedCallback method

Child Component

//child.js
@api 
connectedCallback(){ 
   // my do init logic
}

Parent Component

//parent.js
...
this.template.querySelector('c-child').connectedCallback();
...

Is it good to make connectedCallback() public? Is there any other issue which will affect this modal? If not please give me a good modal for DoInit in LWC

Thanks in advance.

1 Answer 1

3

I haven't tried making the connected callback public, but I have an alternative for this. You can write a separate public method for init. Let us say,

@api
doInit(){
    // do your init logic.
}

You can call this method from connectedcallback like this.

connectedCallback(){ 
   this.doInit();
}

Also, you can call the same method from the parent,

//parent.js
...
this.template.querySelector('c-child').doInit();
...
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .