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

362
Vistas
How to fetch realtime database with useState?

Im trying to get realtime database from firebase using react useState. im a newbie with react especially with hooks. i got error like this Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. . i think this because useState assume onValue give a infinite loop. do i should apply a setTimeOut ? or using another hook ? this my code looks like

import React, { useState } from 'react'
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import {getDatabase,ref,onValue} from 'firebase/database'
import firebaseConfig from '../config/fbConfig'
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries


function SensorCard() {
    const [data,setData] = useState(0);    
    initializeApp(firebaseConfig);
    const db = getDatabase()
    const soilTemp = ref(db, '/sensor/kelembabanTanah')
    onValue(soilTemp, (snapshot) => {
        setData(snapshot.val())
    })
    
  return (
    <>
        <div className='
        border border-green-900 '>
            <div>Temperature</div>
            <div>
                <div>{data}</div>
                <div>C</div>
            </div>

        </div>
    </>
  )
}

export default SensorCard

Thanks

about 4 years ago · Juan Pablo Isaza
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