examples/write-patterns/patterns/2-optimistic-state/README.md
This is an example of an application using:
useOptimistic hookThis allows writes to be displayed locally immediately, by merging temporary optimistic state into the synced todo list before rendering. If the app (or API) is offline, then the writes are retried following a backoff algorithm and should eventually succeed when the app (or API) comes back online.
When the writes do succeed, they are automatically synced back to the app via Electric and the local optimistic state is discarded.
Simple to implement. Allows you use your existing API. Takes the network off the write path. Allows you to create apps that are fast and available offline for both reading and writing data.
Good use-cases include:
The optimistic state is only available within the component that makes the write. This means that other components rendering the same state may not see it and may display stale data. The optimistic state is also not peristent. So it's lost if you unmount the component or reload the page.
These limitations are addressed by the shared persistent optimistic state pattern.
See the How to run section in the example README.