Function: useLogic()
Imported from
@react-logic/coreuseLogic<
T>(logicClass,cleanup?):LogicInstance<T>
Defined in: core/src/lib/use.logic.ts:125
A React hook to use a logic class with dependency injection and state management.
Type Parameters
T
T extends object
The logic-class instance type. Inferred from logicClass.
Parameters
logicClass
LogicClass<T>
The logic class to instantiate and manage.
cleanup?
(instance) => void
An optional cleanup function to run when the component unmounts, receiving the logic instance.
Returns
LogicInstance<T>
The instance of the logic class.
Example
class MyLogic {
count = state(0);
increment() {
this.count(this.count() + 1);
}
}
const MyComponent = () => {
const logic = useLogic(MyLogic);
return (
<div>
<p>Count: {logic.count()}</p>
<button onClick={() => logic.increment()}>Increment</button>
</div>
);
}