Appearance
@shtse8/fluxus / src / computedProvider
Function: computedProvider()
computedProvider<
T>(compute,options?):ComputedProviderInstance<T>
Defined in: src/providers/computedProvider.ts:99
Creates a ComputedProviderInstance which derives its state by computing a value based on other providers.
The computation function is executed lazily when the provider is first read within a scope, and its result is cached. The computed value is automatically re-evaluated when any of the providers it read or watched during the computation change their state.
Type Parameters
T
T
The type of the computed value.
Parameters
compute
(reader) => T
The function that computes the derived state. It receives a ScopeReader to access other providers. It's crucial to use reader.watch or reader.read within this function to establish dependencies correctly for automatic recomputation.
options?
ProviderOptions
Returns
The created ComputedProvider instance.
Example
ts
const countProvider = stateProvider(0);
const doubleCountProvider = computedProvider((reader) => {
const count = reader.watch(countProvider); // Establish dependency
return count * 2;
});See
- stateProvider for creating mutable state.
- ScopeReader for how to access dependencies.