immerjs / use-immer

Use immer to drive state with a React hooks
MIT License
4.04k stars 92 forks source link

Undo Feature #44

Closed ghost closed 4 years ago

ghost commented 4 years ago

So I have a use case in which it would be nice to be able to undo an immer action for optimistic updates. Essentially I'd like to change the local state with the immer hook and then post to my backend to update the database. Should this update fail or be invalid, I'd like to revoke the change to the local state. Is there an undo feature for the hook?

danielkcz commented 4 years ago

I believe you can make that yourself fairly easily, just wrap useImmer and its set method, where you can read the previous state and keep it in some ref and you can recover from it later. I think your use case is too specific to be included directly in a library.