The type of the stored value.
The initial value of the reactive variable.
Optional
options: RvInitOptions<T>Optional configuration for the reactive variable.
A reactive variable function that allows getting, setting, and listening for updates.
// initialize a state variable with initial value set to 0
const positiveVar = rv(0, {
// all options are optional
// define custom `eq` function that will be run on every value set to determine
// whether or not value is going to be updated
eq: (oldValue, newValue) => newValue > oldValue && newValue >= 0,
// define callback that's going to be run on every change
on: val => {}
})
// alternatively, there's a handy function initializer
// all options are identical
const positiveVar = rv.fn(() => 0)
// call variable function with no arguments to get its current value
const currentValue = positiveVar()
// call variable function passing an argument in order to set it
// Won't trigger an update because the values are "equal" under this custom rule.
positiveVar(-3, {
// override the initial `eq` function for this update call.
eq: (oldValue, newValue) => newValue > oldValue
// additionally, you can disable initial `eq` function by passing `false` here
// it will use a default `eq` function which is just a strict check: `===`
eq: false // in this case, update WILL happen
})
// you can also subscribe to value without using any hooks
const unsubscribe = positiveVar.on(newValue => console.log(newValue))
positiveVar(4) // logs: 4
unsubscribe()
positiveVar(5) // there will be no logs
Creates a reactive variable from an initializer function. The function is immediately executed to determine the initial value.
The type of the stored value.
A function that returns the initial value.
Optional
options: RvInitOptions<T>Optional configuration for equality comparison and event listeners.
A reactive variable function.
Creates a reactive variable (RV), allowing value retrieval, updates, and subscriptions.