0

I have a custom lightning page with different tabs, each tab is associated with the corresponding LWC component. it is observed that the LWC of each tab is executing only once.

i.e. on click of tab1 the corresponding LWC is executing and if we switch from tab2 to tab1 again, the LWC of tab1 is not executing.

How to can we trigger the LWC back and forth?

2
  • 1
    Tabs are lazily initialized and remain in memory for as long as the container itself remains loaded. What problem is this causing you? Please read about the X-Y Problem, then edit your question to include the background information for your question.
    – sfdcfox
    Commented Aug 5, 2023 at 2:00
  • @sfdcfox - thanks. Updated the question. Commented Aug 5, 2023 at 2:29

1 Answer 1

0

Each lightning-tab is not rendered until the tab becomes active at least once. At that point, the components are created and placed in the DOM, at which point connectedCallback is called. If you want to call some code when the tab becomes active, expose a function you can call.

<lightning-tabset onactive={handleTabChange}>
  <lightning-tab name="tab1">
    <c-component-a></c-component-a>
  </lightning-tab>
  <lightning-tab name="tab2">
    <c-component-b></c-component-b>
  </lightning-tab>
</lightning-tabset>
handleTabChange(event) {
  if(event.target.firstChild) {
    event.target.firstChild.doSomething();
  }
}

Then, in the child components:

@api doSomething() {
  // do something here
}

You can simplify your connectedCallback to just call that method:

connectedCallback() {
  this.doSomething();
}

In summary, the component with the tab set will need to inform child components that they need to perform some action when the tabs change.

3
  • sfdcfox , rocking always. The lightning record page has individual tabs, not a single LWC with tab sets. So if we place the individual tabs and associated them with LWC, we cannot re-trigger? Commented Aug 5, 2023 at 21:16
  • @indrasenneelam You'll have to use the appropriate mechanisms for detecting refreshes, such as the lightning/refresh event.
    – sfdcfox
    Commented Aug 6, 2023 at 1:42
  • I'll go thought it. Commented Aug 7, 2023 at 3:43

You must log in to answer this question.

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