useServerStateOnce
useServerStateOnce reads a value the server owns as the component builds and
subscribes to nothing: the panel shows the value as it was when it opened.
useServerStateOnce(ref: ServerRef): IntuseServerStateOnce(body: () => Int): IntuseServerStateOnce(body: () => Boolean): BooleanReference
Section titled “Reference”useServerStateOnce(ref)
Section titled “useServerStateOnce(ref)”export const PoisonThenAndNow = (): Component => { const atOpen = useServerStateOnce(Varps.POISON); const now = useServerState(Varps.POISON);Parameters
Section titled “Parameters”ref: AVarps.*,Varbits.*orSkills.*reference, as foruseServerState.
Returns
Section titled “Returns”The value at build time, typed as the reference says — an Int, or the type a
variable is declared to hold.
useServerStateOnce(body)
Section titled “useServerStateOnce(body)”The second form takes an arrow — useServerStateOnce(() => tierFacts(tier)) —
for a value computed from several reads at build time.
Parameters
Section titled “Parameters”body: An expression. Every read inside it is untracked — a variable, a skill, a cell, and everything a called function reads. It need not be a single call.
Returns
Section titled “Returns”The expression’s value at build time.
Caveats
Section titled “Caveats”- It re-reads when the component is rebuilt, e.g. the interface reopens. A setter, new props or another read’s redraw do not re-read it. A prop the body read is the prop at build time.
- There is no setter. If the panel needs its own editable copy, seed a cell:
useState(useServerStateOnce(V)). - A seeded cell with its setter dropped is refused (
TS2E239). Drop theuseStateand use the value. - Inside a render body only (
TS2E236). WriteuseServerStatethere. - No subscribing hook inside the body (
TS2E237).useState,useWatch,useDerived,useInventory,useOnResize,useRerenderOnand a nested once are refused. AuseServerStateinside the body is allowed. - Not inside a
.map()row (TS2E238). Make the row a component.
const [poison] = useState(useServerStateOnce(Varps.POISON)); // no setter: drop the useStateShowing a value as it was when the panel opened
Section titled “Showing a value as it was when the panel opened” <Reading label="Poison on open" value={`${atOpen}`} color={Colors.WHITE} y={0} /> <Reading label="Poison now" value={`${now}`} color={now === atOpen ? Colors.WHITE : Colors.GREEN} y={ROW_H} />Seeding a cell
Section titled “Seeding a cell”export const StaminaChoice = (): Component => { const [stamina, setStamina] = useState(useServerStateOnce(Varbits.STAMINA_ACTIVE));A bar that remembers where the fight began
Section titled “A bar that remembers where the fight began” const maxAtOpen = useServerStateOnce(HudVarbits.MAX_HP);Troubleshooting
Section titled “Troubleshooting”The value never updates
Section titled “The value never updates”If the panel should follow the value, read it with
useServerState.
The value updated when I did not expect it to
Section titled “The value updated when I did not expect it to”Something rebuilt the component.
The compiler refuses my seeded cell
Section titled “The compiler refuses my seeded cell”You dropped the setter. If nothing will ever write the cell, you do not need one: use the read-once value directly.