I have problem with updating my state using useState hook.
So in my "App" component, I declare my array of objects state:
const [panelSettings, setPanelSettings] = useState([
{
title: 'Background',
active: true,
options: [],
updateDaily: true
},
{
title: 'Time and Date',
active: true,
showSeconds: true
},
{
title: 'Weather Forecast',
active: false,
city: null
}])
Then I pass {panelSettings}
and {setPanelSettings}
down to another component, let's call it "Menu".
In this "Menu" component I render each title and have a checkbox next to them, which should set the "active" property. Like so:
{panelSettings.map(element => {
return (
<div key={element.title}>
{element.title}
<input type='checkbox' checked={element.active} onChange={() => setPanelSettings(element.active = !element.active)} />
</div>)
})}
But when I click on any of the checkboxes, I get the error "TypeError: Cannot read property 'active' of undefined". However, it comes from my parent component ("App") and not from the "Menu".
I've tried multiple ways to render out the elements and calling the setPanelSettings
function but nothing has worked so far. I've also logged out the object from the "Menu" component and it seemed like the 'active' property has changed there.