ConrabOpto / mst-query

Query library for mobx-state-tree
MIT License
110 stars 7 forks source link

Add api for optimistic updates that uses functions #48

Closed k-ode closed 1 year ago

k-ode commented 1 year ago

Currently, it's only possible to use optimistic updates with rollback when using optimisticResponse. In a lot of cases, it's much more convenient to simply update models in place with a function call.

An api optimisticUpdate could acheive this and exist along side optimisticResponse.

const [updateTitle, { isLoading }] = useMutation(store.updateTitleMutation);
return (
  <button
    type="button"
    onClick={() => {
       updateTitle({
           request: { title },
           optimisticUpdate() {
               store.setTitle(title);
           }
       });
       setMessage('');
    }}>
    Send
  </button>
);

Unlike optimisticResponse which always rolls back it's changes, this api would only rollback on errors.