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

Minimal reactive box

  • 0.1.0
  • 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

Minimal reactive box

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

interface Todo {
  text: string;
  completed: boolean;
}

const [getTodos, setTodos] = box<Todo[]>([]);

const [countActive] = sel(
  () => getTodos().filter((todo) => !todo.completed).length
);

const [countCompleted] = sel(
  () => getTodos().filter((todo) => todo.completed).length
);

const addTodo = (text: string) => {
  setTodos([...getTodos(), { text, completed: false }]);
};

const switchTodo = (target: Todo) => {
  setTodos(
    getTodos().map((todo) =>
      target === todo
        ? {
            ...todo,
            completed: !todo.completed
          }
        : todo
    )
  );
};

const clearCompleted = () => {
  setTodos(getTodos().filter((todo) => !todo.completed));
};

const observe = <T extends React.FC>(Component: T) =>
  React.memo((props) => {
    const update = React.useState(0)[1];
    const ref = React.useRef<[T, () => void]>();
    if (!ref.current) {
      const [Observed, free] = expr(Component, () =>
        update((v) => (v + 1) % 0xffff)
      );
      ref.current = [Observed, free];
    }
    React.useEffect(() => ref.current![1], []);
    return ref.current[0](props);
  });

const TodoInput = observe(() => {
  const [getText, changeHandler, clickHandler] = React.useMemo(() => {
    const [getText, setText] = box("");
    return [
      getText,
      (ev: React.ChangeEvent<HTMLInputElement>) => setText(ev.target.value),
      () => {
        addTodo(getText());
        setText("");
      }
    ];
  }, []);

  return (
    <div>
      Todo: <input value={getText()} onChange={changeHandler} />
      <button onClick={clickHandler}>+</button>
    </div>
  );
});

const TodoList = observe(() => (
  <ul>
    {getTodos().map((todo, key) => (
      <li key={key}>
        {todo.text}{" "}
        <button onClick={() => switchTodo(todo)}>
          {todo.completed ? "open" : "close"}
        </button>
      </li>
    ))}
  </ul>
));

const TodoFooter = observe(() => {
  const active = countActive();
  const completed = countCompleted();
  return (
    <div>
      {active ? `${active} left ` : null}
      {completed ? (
        <button onClick={clearCompleted}>clear completed</button>
      ) : null}
    </div>
  );
});

export default function App() {
  return (
    <>
      <TodoInput />
      <TodoList />
      <TodoFooter />
    </>
  );
}

Edit on CodeSandbox

npm i reactive-box

Enjoy!

Keywords

FAQs

Package last updated on 15 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