@frondruntime/sorus-react
v0.3.1
Published
React hooks for Sorus: useValue, useValues, useSelect, and a realm provider.
Maintainers
Readme
@frondruntime/sorus-react
React hooks for Sorus, a glitch-free reactive state library for applications. It emphasizes:
- Granular rerenders. A component rerenders only when what it read
changes, under
Object.is. - No wrapper. A render body has no
read, so the hooks are the only way in: there is noobserverwrapper, and the hooks work with the React Compiler. - No provider for a value.
useValueanduseValuesneed none: an observable carries its realm. - Stable identity. A write replaces a box's value rather than changing it in place, so a memoized child sees a new identity exactly when the value changed.
The package imports nothing from Effect. React 19 or later and
@frondruntime/sorus are peer dependencies. Sorus is before 1.0: any
release may change the API. It was called Pinna through 0.2.1.
Packages
| Package | Purpose |
| --- | --- |
| @frondruntime/sorus | The main package: realms, boxes, computed values, atoms, keyed collections, models, views, stages, atomic updates, and watchers. No Effect. |
| @frondruntime/sorus-react | React hooks: useValue, useValues, useSelect. React 19 or later. |
| @frondruntime/sorus-effect | Opt-in, for an application already on Effect: boxes that check an Effect Schema on every write, models decoded from the wire, and Effect programs that react to state. effect 4.1 or a later 4.x. |
| @frondruntime/sorus-core | The raw reactive graph (cells, derived values, atoms, watchers, reactions) with no runtime dependencies, for code that builds on the graph itself. |
Quick start
| Package manager | Command |
| --- | --- |
| Bun | bun add @frondruntime/sorus @frondruntime/sorus-react |
| npm | npm install @frondruntime/sorus @frondruntime/sorus-react |
| pnpm | pnpm add @frondruntime/sorus @frondruntime/sorus-react |
| Yarn | yarn add @frondruntime/sorus @frondruntime/sorus-react |
import { Box, Computed, Realm } from "@frondruntime/sorus";
import { useValue } from "@frondruntime/sorus-react";
const realm = Realm.make();
const $count = Box.make(realm, 0);
const $double = Computed.make(realm, (read) => read($count) * 2);
const increment = () => {
Realm.update(realm, (read, write) => {
write($count, read($count) + 1);
});
};
function Counter() {
const double = useValue($double);
return <button onClick={increment}>{double}</button>;
}useValue needs no provider: the value carries its realm. useSelect runs
view logic in the realm of the nearest RealmProvider and throws without
one. A write goes in an event handler, through Realm.update.
Exports
| Export | Does |
| --- | --- |
| useValue($x) | The value of one observable. Needs no provider. |
| useValues([$a, $b]) | The values of several observables of one realm, as a typed array that keeps its identity until one changes. Needs no provider. |
| useSelect((read) => ...) | The result of view logic over observables, in the realm of the nearest RealmProvider. Throws without one. |
| RealmProvider, useRealm | Give the components below a realm, and read it. |
