Skip to content

Store

Store is an app-wide key-value store for shared data, used by every view. It is a singleton and has no reactivity: views read from it in onResume() and write to it in event handlers.

import { Store } from "jops-core";
const store = Store.singleton();

Learn more: Store.

Store.singleton(): Store

Returns the single Store instance, creating it on the first call. Always get the store through this method.

get(path: string): any

Reads a value by dot-notation path.

Parameters

  • path: for example "user.name".

Returns the stored value, or undefined when the path doesn’t exist.

set(path: string, value: any): void

Writes a value by dot-notation path. Intermediate objects are created as needed.

Parameters

  • path: for example "user.name".
  • value: any value (object, array, string, …). It is stored as is, not copied.
clear(): void

Removes all stored data (resets the store to {}).

Store.singleton().set("user.name", "Alice");
Store.singleton().get("user.name"); // "Alice"
Store.singleton().set("cart.items", [1, 2, 3]);
Store.singleton().clear();

A typical pattern: one screen writes, another reads when it comes to the front.

import { View, Store } from "jops-core";
export default class Profile extends View {
onResume() {
const name = Store.singleton().get("user.name") ?? "Guest";
this.get("name").set(name);
}
}