Skip to content

A reactivity library for JavaScript.

License

Notifications You must be signed in to change notification settings

alinnert/reactive-value

Repository files navigation

@alinnert/reactive

This is a library for reactive values.

The implementation is based on CustomEvent and EventTarget which are available in the Browser and recent server runtimes like Node.js 19 and Deno.

The API is heavily inspired by Recoil but this library is framework agnostic.

Examples

Mutable values

const counter$ = mutableValue(1)

console.log(counter$.value)

counter$.onChange((numberVal) => {
  console.log(numberVal)
})

Computed values

const counter$ = mutableValue(1)
const doubleCounter$ = computedValue([counter$], (num) => num * 2)

console.log(doubleCounter$.value)

doubleCounter$.onChange((doubleCounterValue) => {
  console.log(doubleCounterValue)
})

Automated values

const interval$ = automatedValue((next) => {
  let val = 0
  
  setInterval(() => {
    next(val++)
  }, 1000)
})

console.log(interval$.value)

interval$.onChange((intervalValue) => {
  console.log(intervalValue)
})
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy