Cant unsubscribe from MediaQueryList using removeListener
See original GitHub issueWhich @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:
- Created 2 years ago
- Comments:10 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@flash-me seems like it should be addressed to @DimaPateuk 🙃
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.