Skip to content
// EFFECT DAYS 2026

UiAtom unstable

Shares effect's Atoms with components, as owners of shared state.

share turns an Atom into a SharedKey. A component lists it in reads like any other owner and re-renders when the Atom changes. set and refresh write it with a silent Command. The value comes from the reader's AtomRegistry, where the Atom stays mounted while a reader is. family does the same for an Atom.family, with each reader picking its member.

Example A counter whose count is an Atom

import { Component, UiAtom, UiSchema } from "effect/ui"
import { Schema } from "effect"
import { Atom } from "effect/reactivity"
const countAtom = Atom.make(0).pipe(Atom.keepAlive)
const Count = UiAtom.share(countAtom, { name: "Count", schema: Schema.Number })
const Message = UiSchema.TaggedEnum({ ClickedIncrement: {} })
// Requires `AtomRegistry`: provide `AtomRegistry.layer`, or one registry the app shares.
const Counter = Component.define({
name: "Counter",
model: Schema.Null,
message: Message,
reads: { count: Count }
}).make({
init: () => null,
update: ($) => $.run(Count.set($.reads.count + 1)),
view: (h) =>
h.el("button").on("click", Message.ClickedIncrement()).children(`count: ${h.reads.count}`)
})
7 exports Since v4.2.0 Source

Constructors

family

unstable
Since v4.2.0

Shares an Atom.family as one key. Each reader reads the member that select(model, props) in its reads entry returns the argument for.

Details

When a props change or an update makes select return another argument, the reader moves to that member before it renders, in the same flush. It stays mounted, keeps its Model, and gets no onChange. In the update that changes the selection, $.reads still holds the old member, as $.props holds the old props.

family must return the same Atom for an equal argument, as Atom.family does. set(arg, value) and refresh(arg) put arg in their args, encoded as MessageLog.structural does, so the log shows which member was written. A reader lists a family at most once: to read two members, share the Atom.family again under another name. Otherwise it behaves as share.

Signature

declare const family: {
<Name extends string, Arg, R, W>(family: (arg: Arg) => Writable<R, W>, options: {
readonly name: Name;
readonly schema?: Schema.Codec<R, any>;
}): WritableFamily<Name, Arg, R, W>;
<Name extends string, Arg, A>(family: (arg: Arg) => Atom<A>, options: {
readonly name: Name;
readonly schema?: Schema.Codec<A, any>;
}): Family<Name, Arg, A>;
}

Example One view per todo, picked by id

import { Component, UiAtom, UiSchema } from "effect/ui"
import { Schema } from "effect"
import { Atom } from "effect/reactivity"
const Todo = Schema.Struct({ title: Schema.String, done: Schema.Boolean })
const todoAtom = Atom.family((id: string) =>
Atom.make({ title: id, done: false }).pipe(Atom.keepAlive)
)
const Todos = UiAtom.family(todoAtom, { name: "Todo", schema: Todo })
const Message = UiSchema.TaggedEnum({ Toggled: {} })
const TodoView = Component.define({
name: "TodoView",
props: Schema.Struct({ id: Schema.String }),
model: Schema.Null,
message: Message,
reads: { todo: { owner: Todos, select: (_model, props) => props.id } }
}).make({
init: () => null,
update: ($) =>
$.run(Todos.set($.props.id, { ...$.reads.todo, done: !$.reads.todo.done })),
view: (h) =>
h.el("button").on("click", Message.Toggled()).children(h.reads.todo.title)
})

share

unstable
Since v4.2.0

Shares an Atom with the components that list it in reads.

Details

name names the owner in the log and in Story's shared step, and prefixes the Commands ${name}.set and ${name}.refresh. schema encodes the value in a reader's Update. Without one, and always for the set args (an Atom may take a different type from the one it reads), values are encoded as MessageLog.structural does.

There is no owner above the reader. A reader reads the AtomRegistry in its own services, and set writes the issuer's, so subtrees given different registries stay apart. While a Layer above the reader is still building, the reader reads the registry above that Layer, then moves to the Layer's once it is ready, re-rendering without onChange. It moves the same way when its own keyed Layer is replaced. A reader fails when no ready Layer provides a registry, or when the Atom throws as it's first read. The Atom stays mounted from the first reader until the last one unmounts, so an Atom without Atom.keepAlive or an idle TTL resets then.

In a Story or Scene, give a reader the value with .shared(Key, value).

Signature

declare const share: {
<Name extends string, R, W>(atom: Writable<R, W>, options: {
readonly name: Name;
readonly schema?: Schema.Codec<R, any>;
}): Writable<Name, R, W>;
<Name extends string, A>(atom: Atom<A>, options: {
readonly name: Name;
readonly schema?: Schema.Codec<A, any>;
}): Shared<Name, A>;
}

Models

Family

interface unstable
Since v4.2.0

An Atom.family shared with family, whose readers each pick a member.

Signature

interface Family<in out Name extends string, in out Arg, in out A> extends SharedKey<Name, A, never, never, AtomRegistry.AtomRegistry>, Selecting<Arg> {
readonly family: (arg: Arg) => Atom<A>;
refresh(arg: Arg): Write;
}

Shared

interface unstable
Since v4.2.0

An Atom shared with share: the SharedKey a reader lists in reads, which requires AtomRegistry rather than an owner above.

Signature

interface Shared<in out Name extends string, in out A> extends SharedKey<Name, A, never, never, AtomRegistry.AtomRegistry> {
readonly atom: Atom<A>;
refresh(): Write;
}

Writable

interface unstable
Since v4.2.0

A writable Atom shared with share, which set writes.

Signature

interface Writable<in out Name extends string, in out R, in out W> extends Shared<Name, R> {
readonly atom: Writable<R, W>;
set(value: W): Write;
}

WritableFamily

interface unstable
Since v4.2.0

A family of writable Atoms shared with family, which set writes.

Signature

interface WritableFamily<in out Name extends string, in out Arg, in out R, in out W> extends Family<Name, Arg, R> {
readonly family: (arg: Arg) => Writable<R, W>;
set(arg: Arg, value: W): Write;
}

Write

type unstable
Since v4.2.0

A silent Command that writes an Atom in the issuing component's AtomRegistry.

Signature

type Write = Command.Issued<Component.Noop, AtomRegistry.AtomRegistry, Component.Noop, "Effect">