
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
reactive-box
Advanced tools
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 />
</>
);
}
npm i reactive-box
Enjoy!
FAQs
Minimalistic, fast, and highly efficient reactivity
The npm package reactive-box receives a total of 85 weekly downloads. As such, reactive-box popularity was classified as not popular.
We found that reactive-box demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.