vendredi 24 septembre 2021

Why there's a "delay" when values are being stored using checkbox select?

Something very weird is going on with my code and it does not makes any sense. Every time I save/store some data it has a "delay" like for example if I click 2 checkboxes, when I click the first one nothing happens, when I click the second one it grabs the value of the first one I clicked. let me show you:

enter image description here

Now the same happens when I click the one that selects all of them but it does not affects the maths at least (which is the most important part) here:

enter image description here

if I unselect all it shows that all the values where being saved at least:

enter image description here

now comes the even more weird part if I do one by one then the maths doesn't match and is because is not "adding" the one I click first:

enter image description here

Here is my piece of code I mean technically is working but clearly I'm missing something idk let me know if you require something else:


// "librosnuevos" get updated on the firebase I will not add it cause I think is not necessary 

const [librosNuevos, setLibrosNuevos] = useState([]);
const [LibrosID, setLibrosID] = useState([]);
    const handleChange = (e, data) => {
    const { name, checked } = e.target;
    if (checked) {
      // if cheked and selectall checkbox add all fileds to selectedList
      if (name === "allSelect") {

        setLibrosID(librosNuevos);
        let x = 0
        librosNuevos.map((i) => {
          x += i.precio
          setTPLibrosNuevos(parseFloat(x).toFixed(2))
        })

      } else {
        // if cheked and specific checkbox add specific field to selectedList
        setLibrosID([...LibrosID, data]);

        let x = 0
        LibrosID.map((i) => {
          x += i.precio
          setTPLibrosNuevos(parseFloat(x).toFixed(2))
        })
      }
    } else {
      // if uncheked and selectall checkbox add remove all fileds from selectedList
      if (name === "allSelect") {
        setLibrosID([]);

        let x = 0
        librosNuevos.map((i) => {
          x = 0
          setTPLibrosNuevos(parseFloat(x).toFixed(2))
        })

      } else {
        // if uncheked and specific checkbox remove specific field from selectedList
        let tempuser = LibrosID.filter((item) => item.id !== data.id);
        setLibrosID(tempuser);

        let x = 0
        tempuser.map((i) => {
          x += i.precio
          setTPLibrosNuevos(parseFloat(x).toFixed(2))
        })
      }
    }
    console.log(LibrosID)
  };




Aucun commentaire:

Enregistrer un commentaire