Skip to content

content: Missing clean-up function in useEffect to remove event listener. #3648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
decipher-cs opened this issue May 9, 2024 · 0 comments · Fixed by #3684
Closed

content: Missing clean-up function in useEffect to remove event listener. #3648

decipher-cs opened this issue May 9, 2024 · 0 comments · Fixed by #3684
Labels
content Issues related to the contents of the documentation website

Comments

@decipher-cs
Copy link

URL

https://ionicframework.com/docs/theming/dark-mode#css-class

Issue Description

The example in src/main.tsx for setting dark mode doesn't use a clean-up function inside the useEffect when adding event listener.

useEffect(() => {
    // Listen for changes to the prefers-color-scheme media query
    prefersDark.addEventListener('change', (mediaQuery) => initializeDarkPalette(mediaQuery.matches));
  }, []);

This would just continuously add listeners.

This would fix the issue.

useEffect(() => {
    // Listen for changes to the prefers-color-scheme media query
    prefersDark.addEventListener('change', funct);
    return ()=>{
      prefersDark.removeEventListener('change', funct);
    }
  }, []);
@decipher-cs decipher-cs added the triage New issues label May 9, 2024
@brandyscarney brandyscarney added content Issues related to the contents of the documentation website and removed triage New issues labels Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content Issues related to the contents of the documentation website
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants