DEV Community

Discussion on: React Hook to Allow Undo/Redo

Collapse
 
jeremyling profile image
Jeremy

Yes, definitely. Use any library you prefer as long as the methods are equivalent. Tree shake or import only what you need for production and you're good.

Thank you for the second point! I've had use for deep copies elsewhere in the project and admittedly got lazy here. There isn't a need for a deep copy here since there's no need to mutate the copy contents. Have edited the code to use your suggestions.

For those who read this and lack context, this was the original code in the setState method:

const copy = cloneDeep(states); // Use lodash cloneDeep to get a deep copy
copy.length = index + 1; // This is to remove all future (redo) states after current index

Following LUKESHIRU's suggestion I changed that to:

const copy = states.slice(0, index + 1); // This removes all future (redo) states after current index