useOptimisticAction
useOptimisticAction
const action = useOptimisticAction(typedReference, {
currentData,
updateFn, // your immutable (input, current) => newValue function
...actionOptions,
})
| Additional option | Type | Behavior |
|---|---|---|
currentData | Ref<TData> | Writable authoritative/optimistic state |
updateFn | (input: TInput, current: TData) => TData | Return a new value; do not mutate baseline |
It returns the useAction members plus readonly optimisticData. execute/executeAsync apply the optimistic update before the request. Only one optimistic operation is owned; newer execution replaces older pending work.
Failure, cancellation, or reset restores the baseline only while the ref still contains the installed optimistic value. External ref replacement is preserved. In-place mutations cannot be detected by the identity guard.
Success retains optimistic state until your application merges or refreshes authoritative output. TOutput is never assumed to equal TData. cancel/reset release owned optimism; server-side mutations already underway can still complete. Example and reconciliation guidance.
If updateFn throws, no request starts; executeAsync rejects and execute logs the failure. Keep updateFn pure and immutable.