I ran into the same issue trying to set my theme with local storage. Based on the article "Flicker-free Dark Theme Toggle", it looks like when using local storage, the page is loaded first before the value in local storage can be acquired and gives a flicker effect. I followed the article and used the next-themes npm and everything works for me. If you don't want to use that npm, then maybe switch over to using a global state (like Redux or Vuex) value to store the toggle value. Save the initial value in browser and set the value in global state and refer to that when rendering