Skip to main content

Update value in a dictionnary with React Hooks/ JS

I have a dictionnary in which I want to re-format some data because when I receive them from my database, they are not formatted correctly. To do that, I store them in a dictionnary like so when the page is opened:

const [data, setData] = useState([]);
db.collection('users').doc(auth.currentUser.uid).get().then((response)=> {setData(response.data().appointments)})

and then I try to call a function to update the startDate and endDate keys' value automatically everytime the page is opened:

// This function re-formats and will be used in the next one
    function toDateTime(secs){
      var t = new Date(1970, 0, 1); // Epoch
      t.setSeconds(secs);
      //console.log(t)
      return t
    }

    // This function has to update the value
    function pwease(datis){
      var secondo = datis['startDate']['seconds']
      var secondu = datis['endDate']['seconds']
      var upDatis = {
        ...datis,
        endDate : toDateTime(secondu),
        startDate: toDateTime(secondo)
      }
      setData([...data, upDatis])
      return upDatis;
      
    }
    
    useEffect(()=> data.forEach((elt)=>{pwease(elt); console.log('Heu')}), [])

note: elements of my 'data' object look like that:

{allDay: false,
endDate: nt {seconds: 1669204800, nanoseconds: 0},
id: 0,
startDate: nt {seconds: 1669203000, nanoseconds: 0},
title: "cjlk"}

But I don't know why it doesn't work and the 'Heu' doesn't get printed. Please help cause I've been stuck for days on that. Thanks!

Via Active questions tagged javascript - Stack Overflow https://ift.tt/ADiX0Ms

Comments