I am trying to update my state. I want to be able to update a specific isChecked property when it is updated when a user clicks a checkbox. My state below:
this.state = {
userPermissions: [
{
appName: "Dashboard",
roles: [
{
appId: "1",
statusId: 1,
isChecked: false
},
{
appId: "1",
statusId: 2,
isChecked: true
},
]
},
{
appName: "Finance",
roles: [
{
appId: "2",
statusId: 3,
isChecked: false
},
{
appId: "2",
statusId: 4,
isChecked: true
},
]
}
]
}
This is what I have tried. I have to map twice to get to the roles and then set the conditions, however, I am assuming I am mutating the state with another new one so when I render it, it breaks.
handleCheck = (e, data) => {
const checked = e.target.checked;
this.setState(prevState => ({
userPermissions: prevState.userPermissions
.map(item => item.roles
.map(item => item.appId === data.appId && item.statusId === data.statusId ? { ...item, isChecked: checked } : item))
}))
}
Is there a cleaner way to do this without mutation? Thanks.
