Skip to content

Function: state()

state<T>(initialValue): State<T>

Defined in: state.ts:87

Creates a reactive state variable that notifies registered watchers whenever its value changes.

Type Parameters

T

T

The type of the value handled by the state.

Parameters

initialValue

T

The initial value of the state.

Returns

State<T>

A new State instance.

Example

ts
const counter = state(0)
counter.watch((newVal, oldVal) => {
  facile.write('#count', `Count: ${newVal}`)
})
counter.value = 1 // triggers the watcher
counter.value     // 1