I'm trying to update something inside the scope of the useState setter. This works as I would expect in the following codepen example:
Update value outside of state variable
But as implemented in my application the insideCallCount is getting called twice for every toggleCell call. See below for the relevant portion:
const callCount = useRef(0);
const insideCallCount = useRef(0);
const toggleCell = (i) => {
callCount.current += 1;
setPattern((pattern) => {
let newPattern = deepCopyPattern(pattern);
newPattern[i][selectedSound].on = !newPattern[i][selectedSound].on;
insideCallCount.current += 1;
return newPattern;
});
console.log('callCount: ', callCount.current);
console.log('insideCallCount: ', insideCallCount.current);
};
The console readout looks like this after I 'toggle' a button several times:
callCount: 1
insideCallCount: 0
callCount: 2
insideCallCount: 2
callCount: 3
insideCallCount: 4
callCount: 4
insideCallCount: 6
callCount: 5
insideCallCount: 8
callCount: 6
insideCallCount: 10
My understanding this has something to do with the fact that it's wrapped in a function definition. I tried wrapping the whole thing in a useCallback with values to watch inside the dependency array but that didn't help. What am I missing?