here's an example component that shows this behavior:
const App = () => {
const [testArray] = useState([{key: "hi", value: "Hello "}, {key: "world", value: "world"}]);
const [otherState, setOtherState] = useState("Change");
function renderTestArray() {
return testArray.map(entry => <span key={entry.key}>{entry.value}</span>);
}
function updateTestArray() {
testArray[0].value = testArray[0].value === "Goodbye " ? "Hello " : "Goodbye ";
setOtherState((prevState) => prevState === "Change" ? "Test" : "Change");
}
return (
<Fragment>
<div>
{renderTestArray()}
</div>
<button onClick={updateTestArray}>{otherState}</button>
</Fragment>
);
};
When I press the Change button, I would expect the button label to change but not the "Hello World" message since the setState for that array is not being called. What I don't understand is how React is picking up that the testArray changed and then rendering that change. Also how is it that the testArray change is being persisted in subsequent renders even if its setState is not being called.