snapshot
Returns a deep plain copy of a store's current data.
Returns a deep plain copy of a store's current data. It reads raw data directly, so it does not subscribe the running computation, and the copy is independent of later writes. Cycles and shared objects are preserved. The walk is iterative, so deep data cannot overflow the stack. Use it to serialize a store, send it to a server or keep an undo history.
Import#
ts
import { snapshot } from "@shadow-js/core";
Signature#
ts
function snapshot<T>(store: Store<T>): Mutable<T>
Parameters#
| Name | Description |
|---|---|
store |
A store view. A value that is not plain data is returned as is. |
Returns#
A plain, untracked deep copy.
Examples#
ts
import { snapshot, useStore } from "@shadow-js/core";
const [form, setForm] = useStore({ name: "Ada", tags: ["admin"] });
const saved = snapshot(form);
setForm({ name: "Grace" });
console.log(saved.name, JSON.stringify(saved)); // Ada {"name":"Ada","tags":["admin"]}