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

102
Visualizações
React component re renders infinitely

I'm using a custom hook to fetch data from api , and my dependency array only contain my setStates functions but still it keeps re re rendering.

From what I learned from useEffect hook is that it only re-renders when a state in the dependency array has changed and in that case I'm not depending on any state here is my custom hook.

This hook fetches cart data of current user and it works fine to this point, but I only have references to the cart item inside the cart object. So I did another fetch, the problem arises from here, when I fetch the items they re-render infinitely.

useEffect(() => {
        console.log('checking cart info');
        if (getCartCookie()) {
            console.log('cart exists');
            if (!auth) {
                dispatch(setLoading(false));
                return;
            }
        }
        let mounted = false;
        const getCart = async () => {
            if (!mounted) {
                //if authenticated , get cart if not found create
                if (auth) {
                    if (getCartCookie()) removeCookie('cart', { path: '/' });
                    console.log('Fetched User   : ', user._id);
                    const { cart, err } = await getUserCart(user._id, token);
                    console.log('Fetched User  cart : ', cart);
                    if (err) await createCart();
                    else {
                        const arr = [];
                        for (let i of cart?.items) {
                            console.log('fetcing items......');
                            const { item } = await getItem(i.item);
                            console.log('item : ', item);
                            if (!item) continue;
                            arr.push(item);
                        }
                        dispatch(setItems(arr));
                        setCookie('cart', cart, { path: '/' });
                        removeCookie('cartErr', { path: '/' });
                        dispatch(setCart(cart));
                        dispatch(setCart_c(cart.items.length));
                        removeCookie('cartErr', { path: '/' });
                        console.log('cart state : ', cart);
                    }
                } else {
                    await createCart();
                    removeCookie('cartErr', { path: '/' });
                }
            }
            dispatch(setLoading(false));
        };
        getCart();
        return () => {
            mounted = true;
        };
    }, [dispatch , setCart , setCart_c , setCookie , setItems]);
about 4 years ago · Juan Pablo Isaza
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