Why does my DBSearch function keep looping when pulling data from Firebase?

Viewed 11

New to coding last month and I'm learning JS through React Native.

I've added a Firestore DB to pull data through to my app and put together a call-back function using the useState hook due to it being Async.

My issue is my DBSearch function is now looping infinitely.

Code below:

const [propertyData, setPropertyData] = React.useState(["Well, this is awkward..."])

const colRef = (listName) => collection(db, listName)


const dbSearch = (listName, callBackFuncHere) => {onSnapshot(colRef(listName), (snapshot) => {
    let newList = []
    snapshot.docs.forEach(doc => {
    newList.push({ ...doc.data(), id: doc.id }) // Pulls Object Data and runs for loop to assign to newList Array
     })
     
    callBackFuncHere(newList)
})};

function retFunc(newList){
  setPropertyData(newList)
  console.log('1')
}


dbSearch('propertyList', retFunc)

Is this an improper use for useState? and how should I change my dbSearch function to stop it continually looping?

Apologies if this has been asked a thousand times before, I'm unsure how to clearly articulate and search for my problem.

0 Answers
Related