如何在QwikJS中管理主题值持久化

How manage Theme value persistent in QwikJS

提问人:rodrigo lemus 提问时间:9/3/2023 最后编辑:cmolinarodrigo lemus 更新时间:11/24/2023 访问量:110

问:

我想处理浅色/深色主题,但代码第一次没有在浏览器上运行

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$()

qwik qwikjs

评论

0赞 Harsh Mangalam 9/5/2023
如果你将主题放在 localstorage 中,你总是会遇到默认主题渲染的问题,然后在浏览器中切换到用户首选主题,即使你将使用 useTask$。如果要防止这种情况发生,请将主题配置放在 redis 或服务器上的其他缓存中。

答:

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
虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。- 来自评论