sessionState
sessionState declares a cell several components can share. It lasts
until the player logs out.
sessionState(initial: Int): StateRef<Int>sessionState(initial: String): StateRef<String>Reference
Section titled “Reference”sessionState(initial)
Section titled “sessionState(initial)”Call it at the top level of a file, never inside a component. Read
it with useState.
const sharedAmount = sessionState(0);const SharedToolbar = ({ x, y }: { x: Int; y: Int }): Component => { const [amount, setAmount] = useState(sharedAmount);Parameters
Section titled “Parameters”initial: AnIntor aString. The value before anything sets it.
Returns
Section titled “Returns”A declaration to pass to useState. It is not the value — you cannot read it
directly.
Caveats
Section titled “Caveats”- Declare it at the top level. Inside a component it is refused
(
TS2E124). - Export it if components in other files need it.
- It lasts until logout and is never saved. For something that should survive a
restart, use
persistedState. - It needs no key — rename the variable freely.
- A string cell is one of these. A private
useState('')is refused;sessionState('')is where a string lives.
const Refused = (): Component => { const openTab = sessionState(0); // belongs at the top of the fileSharing a selection across a panel
Section titled “Sharing a selection across a panel”const openTab = sessionState(0); const [tab, setTab] = useState(openTab); const [tab] = useState(openTab);Holding a string
Section titled “Holding a string”const lastChoice = sessionState(''); <text x={48} y={22} w={{ fill: 52 }} h={14} font={Fonts.SMALL} shadow text={chosen.length === 0 ? 'No option chosen yet' : `Chose ${chosen}`} color={Colors.DIM} />Handing a value from one component to another
Section titled “Handing a value from one component to another”const withdrawAmount = sessionState(0);export const Committed = (): Component => { const [amount] = useState(withdrawAmount);The prompt writes it on Enter; the line below reads it; neither is the other’s parent.
Troubleshooting
Section titled “Troubleshooting”My components do not see the same value
Section titled “My components do not see the same value”Check they are reading the same declaration. Two sessionState(0) calls are
two different values, however similar they look — import one from a shared
file rather than declaring one in each.
The value resets when the player logs out
Section titled “The value resets when the player logs out”Use
persistedState if it should
survive.