question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cant unsubscribe from MediaQueryList using removeListener

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

common

Is this a regression?

No

Description

Cant unsubscribe a listener from the MediaQueryList using removeListener

export const BREAKPOINTS: any = {
  screenKey: 500,
  screenKey2: 600,
};

const mqls: any = {
  screenKey: null,
  screenKey2: null,
};

export class AppComponent implements AfterViewInit {
  ngAfterViewInit() {
    Object.keys(mqls).forEach((key) => {
      mqls[key] = window.matchMedia(`(max-width: ${BREAKPOINTS[key]}px)`);
    });
    this.createListeners();
  }

  listener = () => {
    for (const key in mqls) {
      if (mqls[key].matches) {
        console.log('emitted from method', key);
        break;
      }
    }
  }

  createListeners() {
    Object.keys(mqls).forEach((key) => {
      mqls[key].addListener(this.listener);
    });

    setTimeout(() => {
      this.remove();
    }, 5000);
  }

  remove() {
    Object.keys(mqls).forEach((key) => {
      console.log(mqls[key]);
      mqls[key].removeListener(this.listener);
    });

    console.log('removed all listeners');
  }
}

Please provide a link to a minimal reproduction of the bug

https://github.com/DimaPateuk/media-test

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in

Angular CLI: 12.2.3
Node: v12.20.1
Package Manager: npm 6.14.10
OS: ProductName:    Mac OS X
ProductVersion: 10.14.6
BuildVersion:   18G9216

Anything else?

for the latest version of the Angular removeEventListener works as expected

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dimakubacommented, Sep 1, 2021

@flash-me seems like it should be addressed to @DimaPateuk 🙃

0reactions
angular-automatic-lock-bot[bot]commented, Aug 6, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MediaQueryList.removeListener() - Web APIs - MDN Web Docs
The removeListener() method of the MediaQueryList interface removes a listener from the MediaQueryListener.
Read more >
MediaQueryList.prototype.addListener & removeListener are ...
Because addListener and removeListener are deprecated it means that people are more likely to use the alternatives without looking at the issues ...
Read more >
HTML DOM Document removeEventListener() - W3Schools
Definition and Usage. The removeEventListener() method removes an event handler from a document. ... All HTML DOM events are listed in the:
Read more >
matchMedia removeListener doesn't work? - Stack Overflow
You're creating a new media query list each time, so you're not able to remove the listener from the first query. var m...
Read more >
How to use prefers-reduced-motion in React - Josh W Comeau
mediaQueryList.removeEventListener('change', listener);.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found