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

347
Visualizações
Calculate cart by localstorage with SSR in NextJS

I'm trying to create a shopping cart, in 1 component the items are added to the shopping cart that places this data in the localstorage.

The shopping cart component looks like this, first with this code:

   useEffect(()=>{
  
        SaveToShoppingbag();
 
    },[])

SaveToShoppingBag() , looks like this:

let SaveToShoppingbag = () =>{

    if(JSON.parse(localStorage.getItem('Shoppingbag')) != null){
  
        SetShoppingbag(JSON.parse(localStorage.getItem('Shoppingbag')))
       
    }
     
}

Based on that, the following useEffect is triggered:

   useEffect(()=>{
     SetTotalPrice(0)
       TotalPrices= []
       CalculatePrice()
     
    },[Shoppingbag])

Price calculations are correct. If a number of the product changes in the component, this is also neatly settled. Only when I add a product to the shopping cart from the other component (localstorage.getItem('shoppingbag') ) it is not added, which is probably due to the ,[] in the useEffect. However, if I omit this, the price continues to count because it responds to a change in the shopping bag (,[Shoppingbag])

So anyone have a solution? What if an item is added to the localstorage that the useEffect reacts to?

What I've already tried is to add an eventlistner('storage') but the browser doesn't respond to that because it probably has to do with SSR because I'm using NextJS.

about 4 years ago · Santiago Gelvez
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