🤞 Async UI
Learn how to easily use Optimistic UI in asynchronous functions.
Welcome to the documentation where you will learn how to integrate Optimistic UI into your asynchronous functions.
Getting started
We will start with the easiest part of all, setting and then obtaining that value optimistically using useOptimistic.
Example: Set a random value in the cache with the key "key" and update the UI immediately. If something goes wrong, rollback to the previous value.
Like correctlyCode Sandbox
Like without internetCode Sandbox
import { useOptimistic } from "toolkitify/optimistic-ui";
let value = 0;
await useOptimistic.promise(
async () => {
await new Promise((r) => setTimeout(r, 50));
value += 2;
return value;
},
{
optimisticUpdate: () => {
value += 1;
},
rollback: (error) => {
value = 0;
}
}
);
Interfaces
| Parameter | Type | Required | Description |
|---|---|---|---|
Optimistic Function | Function | ✅ | Function to execute |
options | Options | ✅ | Options to use |
Options
| Parameter | Type | Required | Description |
|---|---|---|---|
optimisticUpdate | Function | ❌ | Function to execute |
rollback | Function | ❌ | Function to execute if something goes wrong |
onSuccess | Function | ❌ | Function to execute on success |
onLoading | Function | ❌ | Options to use on loading |
finally | Function | ❌ | Function to execute at the end |