0

I want to ad an eventListener for window resize inside my custom lightning web component but I don't know how to do it

Maybe something like this?

constructor(){
    window.addEventListener('resize', this.handleResize());
}
handleResize(){
    //custom code
}
1

you need to pass the function by reference and not invoke it directly while declaration. Later it will be invoked when the event is triggered. PFB:

connectedCallback() {
    window.addEventListener('resize', this.myFunction);
}
@track count = 0;
myFunction = () => {
    this.count = this.count += 1;
    console.log('resized => ', this.count);
};

Here is the playground link

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.