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

Initialize / load existing pastStates #75

Closed
KrisLau opened this issue Mar 16, 2023 · 9 comments
Closed

Initialize / load existing pastStates #75

KrisLau opened this issue Mar 16, 2023 · 9 comments
Labels
enhancement New feature or request

Comments

@KrisLau
Copy link

KrisLau commented Mar 16, 2023

So I'm using zundo to add undo/redo functionality to my app but each instance of a sports game has its own state history that I need to be able to undo/redo. Is there a way i can save the pastStates and futureStates to my db and load those back into zundo?

@charkour
Copy link
Owner

It's not currently possible, but I love that idea!

Thank you for the suggestion!

@charkour charkour added the enhancement New feature or request label Mar 17, 2023
@charkour
Copy link
Owner

Technically, you should be able to achieve this by using useMyStore.temporal.setState({ pastStates: statesFromDb.past, futureStates: statesFromDb.future })

@KrisLau
Copy link
Author

KrisLau commented Mar 17, 2023

@charkour That worked, thanks so much! If you get the chance to take a look, I might have an enhancement suggestion here: #74 (comment)

@KrisLau KrisLau closed this as completed Mar 17, 2023
@charkour charkour mentioned this issue Apr 23, 2023
2 tasks
@charkour
Copy link
Owner

Hey @KrisLau,

You can now initialize the temporal store with past and future states as of v2.0.0-beta.15. Please let me know if it works for you. Thanks!

@KrisLau
Copy link
Author

KrisLau commented May 3, 2023

@charkour Unfortunately for my use case I can't initializing the past and future states since the user has to select the team and game then I programmatically load a bunch of info into the store from the database! However I just realized that useMyStore.temporal.setState({ pastStates: statesFromDb.past, futureStates: statesFromDb.future }) doesn't work for the future states for some reason. It works if I separate the setState for the past and future states

@charkour
Copy link
Owner

charkour commented May 3, 2023

Gotcha, are you using React? If so, you can use React context to initialize your zustand store after the user selects the state!

@KrisLau
Copy link
Author

KrisLau commented May 3, 2023

@charkour I'm using React Native and yeah that's kind of similar to what I'm doing currently! The only difference is I have 2 contexts for the game because one depends on the value of another.

Also just realized the state is being applied with the setState but futureStates is being wiped when I move to the next screen, really weird since pastStates is fine. Not sure why?

@charkour
Copy link
Owner

charkour commented May 4, 2023

Gotcha, are you using this zustand dependency injection pattern with the new zundo API for initializing history? Using these two together should allow you to achieve your desired behavior and you should no longer be needing to call useMyStore.temporal.setState({ pastStates: statesFromDb.past, futureStates: statesFromDb.future })

@charkour
Copy link
Owner

charkour commented May 4, 2023

For zustand dependency injection, I made a simple helper https://github.com/charkour/zustand-di

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants