charkour / zundo

🍜 undo/redo middleware for zustand. <700 bytes
https://codesandbox.io/s/zundo-2dom9
MIT License
616 stars 20 forks source link

Partialize for Nested Objects in Zundo #170

Open astrarudra opened 7 months ago

astrarudra commented 7 months ago

First and foremost, I would like to express my appreciation for the incredible work you have done with Zundo. It has proven to be an invaluable tool for managing state and implementing undo/redo functionality in Zustand-powered applications.

I am reaching out to raise an issue I've encountered while using Zundo in conjunction with Zustand, particularly concerning the handling of nested objects within the store during undo and redo operations.

In my application's store, I have a structure that includes multiple properties, among which are nested, simple1, and simple2. While implementing undo and redo functionality, I have utilized the partialize function to specify which properties should be considered for linking to undo and redo actions. However, I have encountered a challenge specifically with the nested object.

The nested object contains several properties, such as nestedProp1, nestedProp2, nestedProp3, and nestedProp4. However, for undo and redo operations, I am only interested in tracking changes to nestedProp1 and nestedProp2. Unfortunately, the current implementation of partialize does not provide a straightforward way to achieve this level of granularity.

Is there a way to achieve this?

Here is an example of the current partialize implementation:

partialize: (state) => {

   const { nested, simple1, simple2 } = state;
   const { nestedProp1, nestedProp2 } = nested; // Only interested in these properties for Undo/Redo (only 1 level)
   return { nested, simple1, simple2 };

}

astrarudra commented 6 months ago

Any workaround to do this ? @charkour ?

charkour commented 6 months ago

Hi @astrarudra,

Thank you so much for the kind words and thank you for your patience. Unfortunately, it's not possible to partialize a nested object and I don't think there's a way to change the code to support this.

However, here is a workaround to try. Create a setter in the zustand store that splits nested into tracked and untracked. Your example would change from nested.nestedProp1, nested.nestedProp2, and nested.nestedPropUntracked to tracked.nestedProp1, tracked.nestedProp2, and untracked.nestedPropUntracked.

Hope this helps!

charkour commented 6 months ago

I'll take a look at adding a merge function that would make your desired behavior easier to do by default.

astrarudra commented 6 months ago

Thanks! Ill take a look into the zundo code - Ill try, if i succeed - will send a pull requests :)

charkour commented 6 months ago

Thanks! I did take a look at the zundo code and decided it's likely best to make a middleware to handle how states are merged rather than update zundo itself.

hsavit1 commented 2 months ago

hi is there an example of what to do here? i dont care for changes in all of my object properties, only some of them

charkour commented 2 months ago

Hi @hsavit1, you can check out the documentation on Partialize here.