react-rv
    Preparing search index...

    Function rv

    • Creates a reactive variable (RV), allowing value retrieval, updates, and subscriptions.

      Type Parameters

      • T

        The type of the stored value.

      Parameters

      • val: T

        The initial value of the reactive variable.

      • Optionaloptions: RvInitOptions<T>

        Optional configuration for the reactive variable.

      Returns Rv<T>

      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
    Index

    Methods

    Methods

    • Creates a reactive variable from an initializer function. The function is immediately executed to determine the initial value.

      Type Parameters

      • T

        The type of the stored value.

      Parameters

      • init: () => T

        A function that returns the initial value.

      • Optionaloptions: RvInitOptions<T>

        Optional configuration for equality comparison and event listeners.

      Returns Rv<T>

      A reactive variable function.