1

I have a simple Aura Component that has a LWC component inside it like this:

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <aura:attribute name="recordId" type="String" />
    
    <c:taxCalculator aura:id="taxCalculator" name="xxx" />    

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>    

</aura:component>

The aura controller:

({
    doInit : function(component, event, helper) {
        component.find('taxCalculator').computeTax();
        console.log('FINISHED');
    }
})

The lwc:

import { LightningElement, api, track } from 'lwc';

export default class TaxCalculator extends LightningElement {
  @api name;

  @api
  computeTax() {    
    console.log('HERE');

  }
  
}

metadata of the lwc:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>49.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
      <target>lightning__RecordPage</target>
      <target>lightning__AppPage</target>
      <target>lightning__HomePage</target>
  </targets>
</LightningComponentBundle>

In the console log of the browser I see FINISHED, but not HERE.

1 Answer 1

2

Your code produces the following warning in Chrome:

WARNING: Methods are not available until the component is rendered

logWarning@aura_proddebug.js51519

You have to wait until rendering is complete.

Try:

({
  doInit: function (component, event, helper) {
    setTimeout(
      $A.getCallback(() => component.find("taxCalculator").computeTax())
    );
    console.log("FINISHED");
  },
});

Note that the output will be:

 FINISHED
 HERE

Because it is an asynchronous call.

2
  • as always you are a savior. Thank you. But reagarding the warning message you saw in chrome, I didn't see any. Was there a option you activated, to see?
    – nSv23
    Jan 11, 2021 at 22:18
  • @nSv23 Not that I know of. I just happened to open the console (F12) and saw it.
    – sfdcfox
    Jan 11, 2021 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.