2 ms·
You only need to update the objects that actually changed, and only along the path of the keys that changed. Deep cloning is overkill because you'll incur way t
by phailhaus 4y ago
You only need to update the objects that actually changed, and only along the path of the keys that changed. Deep cloning is overkill because you'll incur way too many updates.
For example:
const [largeArray, setLargeArray] = useState<MyObj[]>([]);
const updateObject = (index: number) => (newObj: MyObj) => {
setLargeArray(prevArray => ([
...prevArray.slice(0, index),
newObj,
...prevArray.slice(index+1),
]));
}
- mrkeen 4y agoAnd if you don't want to write it yourself, use a library.