@fromStore
@fromStore(store, select) is a read-only host field over a Zustand, Redux, or useSyncExternalStore store. Use it when the source of truth is already that store.
- @state / @prop when this host owns the value
createStore(...)when Radiant owns nested data@fromStorewhen other code already writes the store
In the bag: 1
- Mug
The count patches in place (this.$.count). The list re-renders because its shape changed. Add mug and Add lamp call setState. They never write a Radiant field.
Worked recipes: Zustand store and Redux store.
Example
import { RadiantElement, customElement, fromStore } from '@ecopages/radiant';
@customElement('cart-badge')
export class CartBadge extends RadiantElement<{ count: number }> {
@fromStore(cartStore, (state) => state.items.length)
count!: number;
override render() {
return <span>{this.$.count}</span>;
}
}The declared field type is the selected value, and is the type @bindTo passes to map: see Typing Decorated Fields.
Assignment throws. Write through setState, dispatch, or the store's actions. Declare the field without an initializer (count!: number): the store snapshot replaces one, and development builds warn once per class and field.
Arguments
Same arguments as fromExternalStore(store, select?, options?) from @ecopages/signals.
| Argument | Type | Description |
|---|---|---|
store | { subscribe, getSnapshot? | getState?, getServerSnapshot? } | Zustand and Redux use getState. React's contract uses getSnapshot. |
select | (snapshot) => Selected | Optional. Omit it to bind the whole snapshot. |
options.equals | (previous, next) => boolean | Optional. Defaults to Object.is. A selected value equal to the last one does not notify. |
The selector runs once per snapshot, so a notification that leaves the state object the same does not run it. Return a slice of the state (a primitive, state.items). A selector that builds a new object or array notifies on every store change; pass equals to compare it yourself.
What It Changes
- the field holds the selected slice, like
@state, but it has no setter this.$.namepatches that node; a direct read inrender()re-renders when the slice changes@onUpdated('name')and@bindTowork on the field- the host subscribes while connected,
this.$bindings included, releases the store on disconnect, and subscribes again on reconnect, when the bindings catch up - server rendering reads
getServerSnapshot()(or the live snapshot when there is none) and never subscribes
When The Store Arrives Through Context
A field decorator cannot see a store that is not known at class definition. Provide the store object (its identity stays stable). Each descendant registers a slice in @onContextUpdate:
@onContextUpdate({ context: cartContext })
onCart(store: typeof cartStore) {
this.registerReactiveMember(
'count',
fromExternalStore(store, (state) => state.items.length),
);
}@contextSelector is the wrong tool: it reacts when the context value is replaced, and a Zustand store is not replaced on setState.
Server Snapshot
Pass getServerSnapshot when the live snapshot cannot be read on the server, or holds other data there. Server rendering paints from it and never calls getSnapshot or subscribe. Before the first connect, the field reads it too. The host subscribes when it connects, before it hydrates or renders, so the client view shows the live getSnapshot / getState, and the field keeps reading the live snapshot after that, also while the host is detached.
@fromStore(
{
subscribe: store.subscribe,
getSnapshot: () => store.getState(),
getServerSnapshot: () => serverCart,
},
(state) => state.items.length,
)
count!: number;Related
- Zustand store
- Redux store
- External stores —
fromExternalStore(...), context, hydration - @state
- @signal