Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
React context not updating on certain places

On button click I'am adding product information to local storage and setting it to context.

I'm using context in two places - header and products page, syntax is almost the same. There is a console.log in header and whenever i'am interacting with context it's instantly firing, confirming that context is working fine. However, when i enter products page i need to reload it one more time in order to get the data. Why is that?

Context file

import React, { useState, useEffect } from "react";

export const LikedContext = React.createContext([{}, () => {}]);

export const LikedProvider = (props) => {
  const [liked, setLiked] = useState(null);

  useEffect(() => {
    if (typeof window !== "undefined") {
      let likedData = localStorage.getItem("liked");

      likedData = likedData ? JSON.parse(likedData) : "";
      setLiked(likedData);
    }
  }, []);

  return (
    <LikedContext.Provider value={[liked, setLiked]}>
      {props.children}
    </LikedContext.Provider>
  );
};

Header


export const LikedProductsIcon = () => {
  const [liked, setLiked] = useContext(LikedContext);
  console.log("liked header", liked);

  const likedCount =
    null !== liked && Object.keys(liked).length ? liked.totalProductsCount : "";
  const totalPrice =
    null !== liked && Object.keys(liked).length ? liked.totalProductsPrice : "";

  return (
    <>
      <CartContainer to="/pamegti-produktai">
        <CountCircle>
          <HeartSvg />
          {likedCount ? <span>{likedCount}</span> : ""}
        </CountCircle>
      </CartContainer>
    </>
  );
};

Liked products page

const LikedProducts = () => {
  const [liked, setLiked] = useContext(LikedContext);

  console.log("liked page", liked);

  return (
    <Layout>
      <Container>
        {liked ? (
          liked.products.map((product, index) => (
            <ProductsCard product={product} blackText />
          ))
        ) : (
          <div>Pamėgtų produktų nėra.</div>
        )}
      </Container>
    </Layout>
  );
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you render multiple <LikedProviders>, then they will each have their own independent state. Setting state in one of them will not affect the other one. The reason they sync up when you refresh the page is that they both load from local storage and see the same value. But that only happens on load, and they will get out of sync after that.

If you want the state to be shared, then you want to just render one of them. Place it at the top of your component tree (app.js is a good spot) so that it's accessible by all the components that need it, and delete the other one.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda