Skip to main content
Version: 0.1.2

Function: useLogic()

Imported from @react-logic/core

useLogic<T>(logicClass, cleanup?): LogicInstance<T>

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>
);
}