Shared state

5 min·Assumes: state, components

() is
with a name: every component that calls the name gets the same copy.
import { shared, state } from '@markless/core';

export const player = shared(() => {
	const box = state({ track: 'Harbour Lights' });
	return { ...box };
});

shared() takes a function that makes the state, and names it player. Every component that calls player() gets the same track, with no props in between.

Two components read one copy

A shared definition is the function you pass to shared(). Call it from any component, and:

  • every caller reads the same values
  • a change in one caller shows in all of them
  • no prop has to carry it

Press play on either player, then turn one volume knob.

Harbour LightsHarbour Lights
A pen-and-ink web page. In its header sits a small MiniPlayer, and below it a large NowPlaying card, both showing the track Harbour Lights. Press play on either and both progress bars move together; drag either bar and the other jumps to the same spot, because both read one shared() track. Each player also has its own volume knob: turn one and the other stays where it is, because volume is scoped to each player. Escape stops playback and resets the knobs.

Melon Tusk renames his app on every press. The shirt and the mug are two components that never pass anything to each other: each calls brand(), so both reprint at once.

PlaygroundRestored your edit
ChirpChirp
Try this
  1. To do: Press Rename it. Melon renames the app, and the shirt and the mug reprint together.
  2. To do: Put a second <Mug /> beside the first, and press Rename it again. Every mug reads the same copy.
  3. To do: Try breaking it: change { scope: 'app' } to { scope: 'everyone' }. The build stops, because no such scope exists.

One signed-in user for the whole app

A real app shows the same data in places far apart. Here the header shows who is signed in, and a settings form elsewhere edits the name. Neither passes the user to the other: both call viewer(), so there is one user.

Frodough is signed in. Settings, far from the header, lets Smeagle in, and the header follows, though no prop carried the name.

PlaygroundRestored your edit
Try this
  1. To do: Press Sign in as Smeagle. The header changes, and no prop carried the name.
  2. To do: Put a second <Header /> under the button, and press again. Both read the one user.

Who shares one copy

The second argument, the scope, says who shares one copy. You never write down where the sharing starts: the compiler finds it when it builds your app.

  • { scope: 'app' }

    One copy per user: on the server, the request rendering the page; in the browser, until a reload. This is the default.

  • { scope: 'subtree' }

    One copy for the component that calls it first and everything inside it, so each tab group gets its own.

Any other word stops the build, as the last step of the player example shows.

One copy per tab group

A tab group is several components that work as one: a root, triggers and panels. Each tab group needs its own copy, shared by its parts. A subtree is a component plus everything rendered inside it, and with one copy per subtree:

  • the outermost component that calls the definition gets a new copy
  • everything rendered inside it shares that copy
  • place the tab group twice, and you get two copies

Below, Tabs, Tab and Panel all call tabs(). Tabs is the outermost, so each card keeps its own open tab: what Grandalf and Ram claim, and the truth.

PlaygroundRestored your edit

Grandalf

Never late.

Ram

AGI next year.

Try this
  1. To do: Press Truth on Grandalf. Ram stays on Claim.
  2. To do: Add a third card, <Tabs open="Truth">, with its own tabs and panels. It starts with its own copy.

The UI components work the same way, so two menus on one page open and close on their own.

A tab group inside another tab group gets its own copy too. Put a Tabs inside another one's Panel, and the inner Tabs starts a new copy, so its tabs never move the outer ones:

import { Panel, Tab, Tabs } from './tabs.tsrx';

export default function Week() @{
	<Tabs open="Plan">
		<Tab name="Plan" />
		<Panel name="Plan">
			<Tabs open="Monday">
				<Tab name="Monday" />
				<Tab name="Tuesday" />
			</Tabs>
		</Panel>
	</Tabs>
}

A copy lasts until a reload

  • On the server, every request starts from the starting values.
  • In the browser, one copy lives as long as the document, through links, Back and Forward.
  • A reload starts again from the starting values.

Two apps on one page share by name

Two separately built apps on one page that call the same definition read and write one copy. This happens with micro-frontends: parts of one page that different teams build and ship on their own. The name is the file plus its export, like lib/player.tsrx#player, so both must import it from the same place.

Both apps must also give the value the same shape: the same field names and types. To give each app its own copy instead, use { scope: 'container' }.

Coming from React context?

A subtree definition is context with no Provider to write. The outermost component that calls it is the provider, and the compiler sees which one that is at build time. Some component is always the outermost caller, so there is no missing-provider error to guard against.

An app definition is one copy per user. On the server each request gets its own, so two requests rendering at once never share it. In the browser it is kept until a reload, across in-place navigation, and it starts from the values the server rendered.

Use state() when one component owns a value, and shared() when several need it. Next, turn files into pages.