Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
My Darkmode not save in localstorage Svelte

hello devs I'm trying to save my dark mode in localstorage but I can't I've been trying for hours and I don't see documentation that works for me I'm using svelte not sveltekit Sorry for my english i'm not a native and sorry for my ask i am new in development and in stackoverflow

// This is my store but not work the localStorage
import {writable} from 'svelte/store';


const storedTheme = localStorage.getItem("darkmode");
export let darkmode=writable(storedTheme);
darkmode.subscribe(value => {
    localStorage.setItem("darkmode", value===false ? false : true);
});

// and in my switch  archieve i got this
<script>
  import {darkmode} from '../store/Store';
    const handleClick=()=>{
        darkmode.update(darkmode=>!darkmode);
    };
</script>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

localStorage.setItem converts the value to a string before storing it. If you do localStorage.getItem('darkmode') in the JS console then you'll likely get "true" or "false" rather than true or false.

It's hard to know whether this is what's causing your issue without seeing how you are using the value of darkmode in your view. If you're treating it like a Boolean, I would expect it to always be in dark mode, since the strings "true" and "false" both convert to the Boolean value true.

If this is what's happening, the easiest way to fix it would be with something like this:

const storedTheme = localStorage.getItem("darkmode") === "true";

More generally, if you want to store non-string values in localStorage then you may want to consider serializing/deserializing them using JSON.stringify/JSON.parse. This will handle your Boolean case here, as well as more complicated things like an object.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda