Javscript onload in LWC

  • i have a requirement to call a javascript method on load of the template.

    is there any way to do that in LWC?

  • To execute actions when component is rendered, use renderedCallback() method and place the required business logic inside this method.

    // mySampleInit.js
    import { LightningElement } from 'lwc';
    export default class MySampleInit extends LightningElement {
        renderedCallback() {
            //  the required business logic to be executed when component is rendered
        }
    }
    

    This is the correct answer. The upvoted answer from sanket is incorrect IMHO.

    if you are intending to use renderedCallback, You should know that, it will fire after connectedCallback and also it may fire more than once(whenever the component rerender itself).

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM