When I change the property of an object in an array with react hooks, the component does not re-render.
I consulted my lead tech and he explained that React searches "breadth" over "depth," meaning React sees the object, but it can't see changes to the object's properties.
const [array, setArray] = useState([
{'id': 0, text: '0'},
{'id': 1, text: '1'},
{'id': 2, text: '2'}
]);
I expect this the component to re-render when the state is changed, but the behavior acts as if the array of objects were not changed at all.
If I create a copy of array as newArray and change newArray[2].text = '3' and setArray(newArray), react thinks the array hasn't changed. Thus, the component does not reload.
As a workaround, I use
const [trigger, setTrigger] = useState(false);
setTrigger(!trigger);
to force reload. However, this is a hacky way to reload the component and I want a more React way of doing it.
Thank you advance!