Skip to content

Using a Service

Once you’ve created a service, there are a few ways to use it.


You can safely destructure anything in Rimitive from a service, so for simpler use cases, you can export the return of a service:

myService.ts
export const { signal, computed } = compose(SignalModule, ComputedModule);

You can also export the service itself, which is callable:

myService.ts
export const svc = compose(SignalModule, ComputedModule);
export type Service = typeof svc;

Rimitive makes heavy usage of factory functions that look like components or hooks you might be used to from other frameworks:

import { signal } from './myService.ts'
const useCounter = () => {
const count = signal(0);
const increment = () => count(count() + 1);
return { count, increment };
};
const counter = useCounter();
counter.increment();
counter.count(); // 1

This is a plain function that instantiates reactive state and returns an API. In Rimitive, this pattern is called a “behavior”, conventionally prefixed with use*.

If you want to drive a reactive ui with state, Rimitive also has modules and patterns to help you with that.

Next up though, let’s talk more about the behavior pattern.