I have written a simple component to reproduce the problem I am having in a much larger project component.
Say I have an arrayA where I'd like to continuously add the value 1 on every button click and a state arrayB I would like to update on the button click.
export default function UseStateTest(props) {
const arrayA = [];
const [arrayB, setArrayB] = useState([1, 2]);
function handleClick() {
arrayA.push(1);
setArrayB([3, 4]);
console.log(arrayA);
}
return <button onClick={handleClick}>Test</button>;
}
With the setArrayB
there, the code behaves incorrectly and when I click the button: arrayA will always have 1 element 1 (I think, from experience in my other code where I'm having a similar problem, that the code is actually replacing the element instead of pushing).
When I comment the setArrayB
out, the code behaves correctly, with a new element '1' being added to the array on every button click.
Please may someone help me understand what is causing this behaviour.