Skip to content
Apps— browse docs
On this page

Step 3: A shared headline

One line of state, shared by the whole app instance — not a document, a room.

The lobby needs something to say. Not a document someone opens — a line of text every member of the world sees, edited by anyone, converging live. That's useCollabState:

const { data, actions } = useCollabState({
  headline: field.value<string>("Welcome — make this place yours."),
})

Same field shapes a document codec uses, but there's no codec to declare and no coords to pass. The host already knows where your app's shared state lives — one room per app instance in this world — so you just describe the shape and get back data/actions, exactly like useDocument.

Add the hook, and an input that writes to it. Edit the code below:

A shared headline
src/app.tsx Lines 17 to 19 and 25 to 29 highlighted.

Edit and the example re-runs. Tab indents; press Escape to leave the editor.

Running · your edits, live
Starting the example…

You should see: an italic line appears under "Lobby", already reading "Welcome — make this place yours." — that default came from field.value's argument, not from you typing anything. Change it. Reload the page and it's gone (this is one browser tab, not a real world) — but point two real people at the same running app and both see the same line, live, the moment either edits it.

Tip:

Not the document you're looking at

Notice LobbyScreen never called useDocument. The headline isn't stored on any document — it lives in a room the HOST owns, one per app instance in this world. That's what makes it app state rather than document state: every screen this instance renders shares the same headline, the same way every tab of one browser shares localStorage.

Next steps