Skip to content
Discussion options

You must be logged in to vote

Yeah it took me a min to follow but the issue here is that the logic that actually produces the effect is in the toggle. So it only changes when you press the button. That logic needs to be in an effect so that it runs when the page first loads. Whereas your current effect doesn't actually track values so it is only for initialization so it doesn't need to go in an effect at all.

So I changed it so that all things are derived from the signal and the event just toggles it:

const initializeTheme = () => {
  let theme;
  if (typeof localStorage !== "undefined" && localStorage.getItem("theme")) {
    theme = localStorage.getItem("theme") as "light" | "dark";
  } else if (window.matchMedia("(p…

Replies: 5 comments 8 replies

Comment options

You must be logged in to vote
4 replies
@alexwhitmore
Comment options

@lxsmnsyc
Comment options

@ryansolid
Comment options

@G3root
Comment options

Answer selected by alexwhitmore
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@atk
Comment options

@karpov-kir
Comment options

@karpov-kir
Comment options

@atk
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
7 participants