Optimistic updates failing seemingly randomly #2580
Unanswered
gregoralbrecht
asked this question in
Q&A
Replies: 1 comment 2 replies
-
this seems to be purely an issue with that json placeholder api. We create an entry, and the next fetch sometimes doesn't return that entry 🤷 .
what's broken there please?
hmm the discord link works for me... |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey there! 👋 I'm trying to get optimistic updates working with 2 custom hooks, but I must be doing something horribly wrong because some updates just don't appear like I'd expect them to.
For the sake of easy reproduction, I tried finding a public dummy API that supports
POST
requests, so here's a sandbox: https://codesandbox.io/s/musing-keldysh-s2bmn?file=/src/hooks/useAddPost.tsMore specifically, I have 2 main issues with this:
1. Optimistic updates randomly disappear after being added
I'd expect this to just work fine and it does in like 90% of the time. However, sometimes, the optimistically-added post briefly appears in the list, then disappears again until it's refetched some time later. You can see this by adding some random posts and seeing the described behavior.
2. The query is randomly undefined / doesn't exist
At least in this CodeSandbox, the query is sometimes
undefined
. Is this due to it being a dev server? Hot module replacement? I honestly don't know, but sometimesqueryClient.getQueryData()
returns undefined (as mentioned in the docs). Not sure why that is, though, and I don't know how to handle that case for optimistic updates?I'd much appreciate any help here! 🙃
PS: I already looked at the "Optimistic Updates in TypeScript" example and it seems broken.
PPS: I also wanted to ask this on Discord, but the current link results in a "Unable to accept invite"
Beta Was this translation helpful? Give feedback.
All reactions