createOptimistic
Creates an optimistic signal that can be used to optimistically update a value and then revert it back to the previous value at end of transition.
When called with a plain value, creates an optimistic signal with SignalOptions (name, equals, ownedWrite, unobserved).
When called with a function, creates a writable optimistic memo with SignalOptions & MemoOptions (adds id, lazy).
// Plain optimistic signalconst [state, setState] = createOptimistic<T>(value, options?: SignalOptions<T>);// Writable optimistic memo (function overload)const [state, setState] = createOptimistic<T>(fn, options?: SignalOptions<T> & MemoOptions<T>);Import
import { createOptimistic } from "solid-js";Type signature
function createOptimistic<T>(): Signal<T | undefined>;function createOptimistic<T>( value: Exclude<T, Function>, options?: SignalOptions<T>): Signal<T>;function createOptimistic<T>( fn: ComputeFunction<T>, options?: SignalOptions<T> & MemoOptions<T>): Signal<T>;Parameters
value
Initial value of the signal; if empty, the signal's type will automatically extended with undefined
options
Optional object with a name for debugging purposes and equals, a comparator function for the previous and next value to allow fine-grained control over the reactivity
Return value
[state: Accessor<T>, setState: Setter<T>]
Examples
const [todos, setTodos] = createOptimistic(initialTodos);
const addTodo = action(function* (text: string) { const tempId = crypto.randomUUID(); setTodos((t) => [...t, { id: tempId, text, pending: true }]); // optimistic const saved = yield api.createTodo(text); setTodos((t) => t.map((x) => (x.id === tempId ? saved : x))); // reconcile});