optimistic update syntax
imperative optimistic updates have been tedious and brittle compared to declarative in my experience, is all
AFAICT the declarative example only grows by a handful of lines to account for multiple mutations if I wanted that
3 Replies
I tried to write up the version of this using
withOptimisticUpdate
and was curious which parts felt brittle and tedious here:
In my mind, this mostly feels like a difference of whether you want to write your logic near each mutation vs. near each query (like the code snippet you shared earlier).
(one of the benefits for writing the logic for each mutation instead of for each query comes into play if you have the same query loaded in multiple places like useQuery(api.users.currentUser)
and you want to update all of them so they're all consistent with each other)I mean, if I had to say, the declarative version just maps in my brain better :akkoshrug: writing it in terms of the current query data + mutations in flight
I will say that Convex's current optimistic update API does a better job than others I've worked with (React Query)
Yeah fair that one framing can make more sense vs. the other (almost based on personal preference). I think we're basically talking about the difference between "when I call the create mutation, I add something to all the list queries" vs "when I load the list query, I add something if I have any pending create mutations" (IMO these seem equally declarative / imperative but are just different framings ¯\_(ツ)_/¯)