@react-logic/core
v0.1.2
Published
`useLogic` — bind a plain logic class to a React component, with automatic re-renders driven by signals.
Readme
@react-logic/core
useLogic — bind a plain logic class to a React component, with automatic re-renders driven by signals.
Part of react-logic. For the full toolkit in one install, use @react-logic/react-logic.
Install
npm install @react-logic/core @react-logic/state @react-logic/diPeer dependency: react@^18 || ^19.
Usage
import { useLogic } from '@react-logic/core';
import { state } from '@react-logic/state';
class CounterLogic {
count = state(0);
inc() { this.count(this.count() + 1); }
}
export function Counter() {
const logic = useLogic(CounterLogic);
return <button onClick={() => logic.inc()}>{logic.count()}</button>;
}- One logic instance per component mount.
- Re-renders when any signal read during render changes.
- Optional cleanup callback:
useLogic(LogicClass, (instance) => { ... }).
Testing helpers
import { createTestInjectionScope, flushAsyncSignals } from '@react-logic/core/testing';See the project README for full docs and demos.
License
MIT
