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

106
Visualizações
Clear old data on rendering data from firebase react native

I am trying to fetch data from firebase but I get duplicate data on the app. I clear the state of orders before fetching and then combine all orders from different customers but the problem is that, I get prev. orders and prev. orders + new orders all combined. What I want prev. order + new order only.

  useEffect(() => {
    let customerList1 = [];
    const unsubscribe = firebase
      .database()
      .ref(`/serviceProvider/${user1.uid}/franchise/customers`)
      .orderByKey()
      .on("value", (snapshot) => {
        customerList1.push(snapshot?.val());
        setCustomerList(customerList1);
        // setCustomerList(snapshot.val());
      });
      setShowloading(false);
    return unsubscribe;
  }, [user1.uid]);

  useEffect(() => {
    const innerSubscriptions = [];
     // let orderList1 = [];
    
    if (customerList) {
      setOrders([]); // <-- clear orders 
      Object.values(customerList).forEach((customer) => {
        if (customer) {
          Object.values(customer).forEach((id) => {
            const unsubscribe = firebase
              .database()
              .ref(`/orders/${id}`)
              .orderByKey()
              .on("value", (snapshot, key) => {
                // Grab all orders from the customers and merge
                const order = snapshot?.val();
                // setOrders([...orders, order]); // realtime but gets only last customerList orders
                setOrders((orders) => [...orders, order]); // realtime all orders but reapeated the list and new orders
                // setOrders((orders) => {
                //   return [...orders, order]
                // });
                setShowloading(false);
              });
            innerSubscriptions.push(unsubscribe);
          });
        }
        
      });
    }
    return () => {
      innerSubscriptions.forEach((unsubscribe) => unsubscribe);
    };
  }, [customerList]);
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