From 39048a5d1e8cc2f338e2439eb5ebf7cde1e1eeb3 Mon Sep 17 00:00:00 2001 From: typist <1003659191@qq.com> Date: Wed, 19 Jun 2024 14:08:30 +0800 Subject: [PATCH] fix: clean query listener --- app/components/home.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/components/home.tsx b/app/components/home.tsx index 732706cfd20..15b8cda8bf6 100644 --- a/app/components/home.tsx +++ b/app/components/home.tsx @@ -92,9 +92,13 @@ export function useSwitchTheme() { const query = window.matchMedia("(prefers-color-scheme: dark)"); meta.setAttribute("content", query.matches ? "#000000" : "#FFFFFF"); - query.addEventListener("change", () => { + const updateThemeColor = (e: MediaQueryListEvent) => { meta?.setAttribute("content", query.matches ? "#000000" : "#FFFFFF"); - }); + }; + query.addEventListener("change", updateThemeColor); + return () => { + query.removeEventListener("change", updateThemeColor); + }; } else { const themeColor = getCSSVar("--theme-color"); metaDescriptionDark?.setAttribute("content", themeColor);