@bytesoftio/value
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

@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.

Readme

Keywords

none

Package Sidebar

Install

npm i @bytesoftio/value

Weekly Downloads

1

Version

3.0.0

License

MIT

Unpacked Size

8.29 kB

Total Files

16

Last publish

Collaborators

  • maximkott