I have an object in a useState
as this:
const [sidebarItems, setSidebarItems] = useState([
{
id: 1,
name: 'داشبورد',
icon: <Item1 />,
active: true
}, ...];
I use the find
method to get an object in the state and change it like this:
const handleToggle = (id) => {
const activeMainItem = sidebarItems.find(i => i.active === true);
activeMainItem.active = false;
}
I thought I should use setSidebarItems
to change the array in the state but my code works well.
Can you please explain to why this happens and doesn't need any setState
?