'React useEffect is not getting triggered

Here is my code:

state initialization

const [uncheckedColArr, setUncheckedColArr] = useState([]);

Control is reaching to the following function, and also consoling correctly.

const updateFilterValue = (columnName, fieldValue, checked) => {
    let tempUncheckedColArr = uncheckedColArr;
    if (tempUncheckedColArr[columnName] === undefined) {
        tempUncheckedColArr[columnName] = [];
    }
    tempUncheckedColArr[columnName] = _.xor(
        tempUncheckedColArr?.[columnName],
        [fieldValue]
    );
    console.log("uncheckedColArr1", uncheckedColArr);

    setUncheckedColArr(tempUncheckedColArr);
    console.log("tempUncheckedColArr", tempUncheckedColArr);
    console.log("uncheckedColArr2", uncheckedColArr);
};

But, it's not triggering following useEffect whenever unCheckedArr is updated from above function

useEffect(() => {
    console.log("uncheckedColArr", uncheckedColArr);
    uncheckedColArr.map((col) => {
        console.log("col", col);
    });
}, [uncheckedColArr]);



Solution 1:[1]

Can you try to copy uncheckedColArr without referance save?

let tempUncheckedColArr = [...uncheckedColArr];

Solution 2:[2]

The update function is asyncronous; it will take some time to complete. If you want to wait until the update is done, then you will need to use the promise it returns:

var updates = {};
updates['/users/' + document.getElementById('username').value] = data;
firebase.database().ref().update(updates)
  .then(() => {
    console.log('Saved successfully');
    location.replace('nextpage.html');
  });

Or with async/await:

async function someFunction () {
  var updates = {};
  updates['/users/' + document.getElementById('username').value] = data;
  await firebase.database().ref().update(updates);
  console.log("Saved successfully")
  location.replace("nextpage.html");
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arthur Arakelyan
Solution 2 Nicholas Tower