React 18

Last Updated: 10/14/2023

Update Object Immutable

  • When updating objects or arrays, we should treat them as immutable/read-only objects. Instead of mutating them, we should create new objects or arrays to update the state.
  • Just like props we should treat state object as immutable/read-only
//create item
const [item, setItem] = useState({ title:  "Coffee", price:  10 });

//update item
setItem({...item, price: 20})