I don't understand why a component only re-renders when using spread operator in an array state item as follows:
I first create a dynamic array state with false values:
const [openList, setOpenList] = React.useState(props.navBarItems.map((item, index) => false));
Option 1
function handleClick(index) {
let newOpenList = [...openList]
newOpenList[index] = !openList[index]
console.log(newOpenList) // prints the same in both options
setOpenList(newOpenList);
}
Option 2:
function handleClick(index) {
let newOpenList = openList
newOpenList[index] = !openList[index]
console.log(newOpenList) // prints the same in both options
setOpenList(newOpenList);
}
I used a console.log in render() (there's probably a better way to know if component updated using hooks) and it is only called when using option 1.