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

195
Visualizações
Local State variable not changing CSS in Next.js upon page load?

So I have a variable in localStorage that is changed upon using a toggle button. When I click the toggle button it correctly changes the color. However, upon refreshing the page it does not respect the color in local storage.

I'm even logging the variable and it's the correct variable - it's just not respecting it in CSS for some reason. Here's the code:

export const Home: React.VFC<any> = () => {
  const theme = useRecoilValue<any>(themeAtom); // gets theme value from the store, which is synced with a localstorage effect

  console.log('theme context from home: ', theme) // dark
  return (
    <>
      <div
        className={`fixed top-0 left-0 h-full w-1/2 bg-${
          theme === 'dark' ? 'black' : 'white' // renders as white
        }
        `}
      />
...

The strange thing is, this technique is working for different contexts, like the user context or authentication context. However, specifically for CSS this is not working.

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

0

Because of how the TailwindCSS JIT engine works, if you're using bg-${/* ... */}, TailwindCSS will not detect that class and won't include it in your final build. Documentation

Instead, include bg in your ternary so Tailwind can detect the class and add it.

<div
  className={`fixed top-0 left-0 h-full w-1/2 ${
    theme === 'dark' ? 'bg-black' : 'bg-white'
    }
  `}
/>
about 4 years ago · Juan Pablo Isaza Relatório

0

I am facing the same issue. Think local-storage is not available in server side and thus not rendering the HTML correctly, what is frustrating that at client side, variables resolved correctly but somehow its not updating/hydrating the html.

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