Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

355
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!