Options
All
  • Public
  • Public/Protected
  • All
Menu

@bytesoftio/value

Installation

yarn add @bytesoftio/value ornpm install @bytesoftio/value

Table of contents

Description

This package provides a generic observable value that can be used in any environment. Additionally there is the @bytesoftio/use-value package that provides a seemless integration with React hooks. This allows you to write services that would work inside React, leveraging its hooks api, but also outside of React. It provides a very simple, observable like object ObservableValue.

createValue

Create a simple observable value that can be hooked into any functional component. Returns an instance of ObservableValue.

import { createValue } from "@bytesoftio/value"

const count = createValue(0)

ObservableValue

Whenever you create a new value through createValue, a new instance of ObservableValue is created behind the scenes. It provides some convenient methods for working with data.

import React from "react"
import { createValue } from "@bytesoftio/value"

const globalCount = createValue(0)

// get current value of 0
globalCount.get()

// change value to 3
globalCount.set(3)

// reset value back to its initial value of  0
globalCount.reset()

// change initial value to 2 and reset value
globalCount.reset(2)

// listen to value changes outside of React
globalCount.listen((value) => console.log(value))

Usage in React

To learn how to use this package in combination with React, please refer to @bytesoftio/use-value package.

Index

Type aliases

CreateValue

CreateValue: <TValue>(initialValue: TValue) => ObservableValue<TValue>

Type declaration

ValueCallback

ValueCallback<TValue>: (newValue: TValue) => void

Type parameters

  • TValue

Type declaration

    • (newValue: TValue): void
    • Parameters

      • newValue: TValue

      Returns void

ValueCallbackUnsubscribe

ValueCallbackUnsubscribe: () => void

Type declaration

    • (): void
    • Returns void

ValueDiffer

ValueDiffer<TValue>: (oldValue: TValue, newValue: TValue) => boolean

Type parameters

  • TValue

Type declaration

    • (oldValue: TValue, newValue: TValue): boolean
    • Parameters

      • oldValue: TValue
      • newValue: TValue

      Returns boolean

Functions

Const createValue

  • createValue(initialValue?: undefined | TValue): Value<undefined | TValue>
  • Parameters

    • Optional initialValue: undefined | TValue

    Returns Value<undefined | TValue>