0

Trying to retrieve a list of numbers for which will be used later on to retrieve more data.

const [listData, setList] = React.useState();

  let tempSim = Temp();

  const setLog = async() => {
    const array = [];
    const date = getDate();
    const dbCon = database.ref('Conditions/Log/Temp/' + date);
    await dbCon.get().then((snapshot => {
      snapshot.forEach(data => {
        array.push(data)
      })
    }))
    dbCon.off()
    console.log(array)
    setList(array)
    console.log(listData)
  }

The array prints fine with all the correct data, however as soon as I set the state and try print that the only output I've been able to get is undefined?

SergeiBU
  • 1
  • 1
  • setState is async, you can't log the value out straight after, but it will be updated in the next render – Dominic May 04 '22 at 10:39
  • See here for a detailed explanation: https://stackoverflow.com/a/54069332/4340854 – Thijs May 04 '22 at 10:40

0 Answers0