React 18

Last Updated: 10/14/2023

Update Object Arrays Immutably

  • We don't need to create a new copy of every object in the array, only for the object that is modified
const [bugs, setBugs] = useState([
	{id: 1, title: "Bug 1", fixed: false},
	{id: 2, title: "Bug 2", fixed: false},
	{id: 3, title: "Bug 3", fixed: false}	
]);

const handleOnClick = () => {
	setBugs(bugs.map(b => b.id === 1 ? {...b, fixed: true}: b));
}