skip to Main Content

so I’m facing a non logical problem!

I have an object and i’m trying to update a value nested too deep inside this object.The value is type of number. I want to decrease the number by one for click, but when i try to do this on that object it decreases by two for click.

This is my code:

const [newDhomatData, setNewDhomatData] = useState(() => {
const obj = { ...dhomatFirebase[0] };

for (let i = 1; i <= ditQendrimiArray.length; i++) {
 obj.muajt[`${muaj}`].datatDhomat[`dat${i}`][`${dataForSpecific.dhoma}`] -= 1; 
}
return obj;
});

And this is the object:

enter image description here

So for example when i run the code above the value of let’s say dhomCift instead of being 44 it becomes 43!

The path of object is right but I have about 1 hour trying to solve this thing and it looks not logical because I’m decreasing the value of this object key by 1 every click but it get decreased by two.

Thanks very much if someone will help.

2

Answers


  1. Chosen as BEST ANSWER

    I found an alternative way of doing this without getting problems

    const [newDhomatData, setNewDhomatData] = useState(() => {
    const obj = {
      ...dhomatFirebase[0],
      muajt: {
        ...dhomatFirebase[0].muajt,
        qershor: {
          datatDhomat: {
            ...dhomatFirebase[0].muajt.qershor.datatDhomat,
            dat1: {
              ...dhomatFirebase[0].muajt.qershor.datatDhomat.dat1,
              dhomFamiljare:
                dhomatFirebase[0].muajt.qershor.datatDhomat.dat1.dhomFamiljare -
                1,
            },
          },
        },
      },
    };
    return obj;
    

    })

    instead of updating the object after declaring it i can update the object on the time that i will declare it and it will give me the result that i want!

    This may be helpfull to someone.


  2. This is probably caused by a StrictMode component which is wrapping your application. You might find it in your main App.js file or in the ReactDOM render method.

    Strict Mode enables additional development behaviors and warnings for the component tree inside it and causes some hooks to run twice.

    More info about it here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search