Given the announcement of "The Future of Lodash", with Lodash joining OpenJS, I thought I'd express my ongoing love for Lodash and share one of the many reasons why it is still useful for me day-to-day.
TL;DR Merge is an unsung hero of the library.
In the absence of something like Immer, if you need to immutably update a deeply-nested object, you can do this:
const properties = { /* ... */ };
setTracks((prev) => ({
...prev,
[trackId]: {
...prev[trackId],
points: {
...prev[trackId].points,
[pointId]: {
...prev[trackId].points[pointId],
properties: {
...prev[trackId].points[pointId].properties,
...properties,
}
}
}
}
}));
Or you can do this:
setTracks((prev) =>
merge({}, prev, {[routeId]: {points: {[pointId]: {properties}}}}))
);
Top comments (0)