Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partialize for Nested Objects in Zundo #170

Open
astrarudra opened this issue Mar 18, 2024 · 7 comments
Open

Partialize for Nested Objects in Zundo #170

astrarudra opened this issue Mar 18, 2024 · 7 comments
Assignees
Labels
question Further information is requested

Comments

@astrarudra
Copy link

astrarudra commented Mar 18, 2024

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
Copy link
Author

Any workaround to do this ? @charkour ?

@charkour charkour self-assigned this Apr 6, 2024
@charkour charkour added the question Further information is requested label Apr 6, 2024
@charkour
Copy link
Owner

charkour commented Apr 6, 2024

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
Copy link
Owner

charkour commented Apr 6, 2024

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

@astrarudra
Copy link
Author

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

@charkour
Copy link
Owner

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
Copy link

hsavit1 commented Aug 19, 2024

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants