提问人:rodrigo lemus 提问时间:9/3/2023 最后编辑:cmolinarodrigo lemus 更新时间:11/24/2023 访问量:110
如何在QwikJS中管理主题值持久化
How manage Theme value persistent in QwikJS
问:
我想处理浅色/深色主题,但代码第一次没有在浏览器上运行
useTask$(({ track }) => {
track(() => themeAppearance.value);
if (isBrowser) {
const savedTheme = localStorage.getItem("themeApp");
console.log("from local storage", savedTheme);
if (savedTheme) {
themeAppearance.value =
savedTheme === "dark" ? ThemeTypes.Dark : ThemeTypes.Ligth;
return;
}
}
});
仅当我使用带有此函数的按钮更改 themeAppearance 时 useThemeHook
const toggleAppearance$ = $(() => {
console.log(themeAppearance.value);
if (themeAppearance.value === ThemeTypes.Dark) {
themeAppearance.value = ThemeTypes.Ligth;
backGroundPrimaryColor.value = BackdrounColor.Ligth;
backGroundSecondaryColor.value = BackdrounColor.Ligth;
textColorPrimary.value = TextColor.HardTextLigthBackground;
textColorSecondary.value = TextColor.LigthTextLigthBackground;
localStorage.setItem("themeApp", ThemeTypes.Ligth);
return;
}
if (
themeAppearance.value === ThemeTypes.Ligth &&
panelBackground === "solid"
) {
themeAppearance.value = ThemeTypes.Dark;
backGroundPrimaryColor.value = BackdrounColor.DarkPrimarySolid;
backGroundSecondaryColor.value = BackdrounColor.DarkSecondarySolid;
textColorPrimary.value = TextColor.HardTextDarkBackground;
textColorSecondary.value = TextColor.LigthTextDarkBackground;
localStorage.setItem("themeApp", ThemeTypes.Dark);
return;
}
if (
themeAppearance.value === ThemeTypes.Ligth &&
panelBackground === "translucent"
) {
themeAppearance.value = ThemeTypes.Dark;
backGroundPrimaryColor.value = BackdrounColor.DarkPrimaryTranslucent;
backGroundSecondaryColor.value = BackdrounColor.DarkSecondaryTranslucent;
textColorPrimary.value = TextColor.HardTextDarkBackground;
textColorSecondary.value = TextColor.LigthTextDarkBackground;
localStorage.setItem("themeApp", ThemeTypes.Dark);
return;
}
});
我尝试使用,但它对我不起作用,因为它在第一次渲染后运行,所以它的用户体验会很糟糕,它的第一个渲染默认主题为深色,然后更改为浅色保存的主题useVisibleTask$()
答:
0赞
cmolina
9/5/2023
#1
你有没有试过启用热切?
useVisibleTask$(
() => {
const savedTheme = localStorage.getItem("themeApp");
console.log("from local storage", savedTheme);
if (savedTheme) {
themeAppearance.value =
savedTheme === "dark" ? ThemeTypes.Dark : ThemeTypes.Ligth;
return;
}
},
{ strategy: 'document-ready' }
);
-3赞
Evgenii Fedotov
11/24/2023
#2
您可以尝试以下解决方法: https://github.com/BuilderIO/qwik/issues/3391
评论
0赞
Adriaan
11/24/2023
欢迎提供指向解决方案的链接,但请确保您的答案在没有它的情况下是有用的:在链接周围添加上下文,以便您的其他用户对它是什么以及它为什么存在有所了解,然后引用您要链接到的页面中最相关的部分,以防目标页面不可用。只不过是链接的答案可能会被删除。
0赞
Michael M.
11/24/2023
虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。- 来自评论
评论