@bytesoftio/value
Installation
yarn add @bytesoftio/value or
npm 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.