Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

reactive-box

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reactive-box

Minimalistic, fast, and highly efficient reactivity

  • 0.2.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
199
decreased by-55.78%
Maintainers
1
Weekly downloads
 
Created
Source

npm version bundle size code coverage typescript supported

Minimalistic, fast, and highly efficient reactivity.

API of library implemented by only thee functions:

  • box - is the container for an immutable value.
  • sel - is the cached selector (or computed value in another terminology) who will mark for recalculating on the next call If some of read inside boxes or selectors changed.
  • expr - is the expression who detects all boxes and selectors read inside and reacted If some of them changed.

Example with React:

import React from "react";
import { box, sel, expr } from "reactive-box";

const [getCounter, setCounter] = box(0);
const [getNext] = sel(() => getCounter() + 1);

const increment = () => setCounter(getCounter() + 1);
const decrement = () => setCounter(getCounter() - 1);

const useForceUpdate = () => {
  return React.useReducer(() => [], [])[1];
};

const observe = <T extends React.FC<P>, P>(Component: T) =>
  React.memo((props: P) => {
    const forceUpdate = useForceUpdate();
    const ref = React.useRef<[T, () => void]>();
    if (!ref.current) {
      ref.current = expr(Component, forceUpdate);
    }
    React.useEffect(() => ref.current![1], []);
    return ref.current[0](props);
  });

const Counter = observe(() => (
  <p>
    Counter: {getCounter()} (next value: {getNext()})
  </p>
));

const Buttons = () => (
  <p>
    <button onClick={decrement}>Prev</button>
    <button onClick={increment}>Next</button>
  </p>
);

export const App = () => (
  <>
    <Counter />
    <Buttons />
  </>
);

Edit on CodeSandbox

More examples

Install

npm i reactive-box

Enjoy!

Keywords

FAQs

Package last updated on 23 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc