@view-models/preact
v1.1.0
Published
Preact integration for @view-models/core
Maintainers
Readme
@view-models/preact
Preact integration for @view-models/core.
Installation
npm install @view-models/preact @view-models/coreUsage
The useModelState hook allows you to subscribe to state updates from a ViewModel instance.
import { ViewModel } from "@view-models/core";
import { useModelState } from "@view-models/preact";
type CounterState = Readonly<{ count: number }>;
class CounterViewModel extends ViewModel<CounterState> {
increment = () => {
this.update(({ count }) => ({ count: count + 1 }));
};
decrement = () => {
this.update(({ count }) => ({ count: count - 1 }));
};
}
const counterModel = new CounterViewModel({ count: 0 });
function Counter() {
const { count } = useModelState(counterModel);
return (
<div>
<p>Count: {count}</p>
<button onClick={counterModel.increment}>+</button>
<button onClick={counterModel.decrement}>-</button>
</div>
);
}Creating view models inside components
When you need to create a view model from within a Preact component, use useMemo to ensure the model is only created once.
It's good practice to provide both a component that creates its own model and a customizable version that accepts a model as a prop:
import { useMemo } from "preact/hooks";
import { ViewModel } from "@view-models/core";
import { useModelState } from "@view-models/preact";
type CounterState = Readonly<{ count: number }>;
class CounterViewModel extends ViewModel<CounterState> {
increment = () => {
this.update(({ count }) => ({ count: count + 1 }));
};
decrement = () => {
this.update(({ count }) => ({ count: count - 1 }));
};
}
// Customizable version that accepts a model
function CustomCounter({ model }: { model: CounterViewModel }) {
const { count } = useModelState(model);
return (
<div>
<p>Count: {count}</p>
<button onClick={model.increment}>+</button>
<button onClick={model.decrement}>-</button>
</div>
);
}
// Simple version that creates its own model
function Counter() {
const model = useMemo(() => new CounterViewModel({ count: 0 }), []);
return <CustomCounter model={model} />;
}API
useModelState<T>(model: ViewModel<T>): T
A Preact hook that subscribes to state updates from a ViewModel instance.
Parameters:
model: A ViewModel instance to subscribe to
Returns:
- The current state of the ViewModel
Features:
- Automatically subscribes to state updates when the component mounts
- Automatically unsubscribes when the component unmounts
- Re-renders the component when the ViewModel state changes
- Multiple components can subscribe to the same ViewModel
License
MIT License
Copyright (c) 2026 Sune Simonsen [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
