@mongez/react-atom
Version:
A simple state management tool for React Js.
91 lines (90 loc) • 3.27 kB
text/typescript
import React from "react";
import { AtomStore } from "@mongez/atom";
//#region ../@mongez/react-atom/src/ssr.d.ts
/**
* The default DOM id used by {@link HydrateAtomsScript} and
* {@link readHydration}. Override per-provider if you need to embed
* multiple snapshots in one document.
*/
declare const DEFAULT_HYDRATION_SCRIPT_ID = "__mongez_atom_state";
/**
* Build a JSON string suitable for embedding inside an HTML `<script>`
* tag. Two safety steps beyond a plain `JSON.stringify`:
*
* 1. The closing tag sequence `</` is escaped to `<\/` so an atom value
* containing literal HTML cannot break out of the script element.
* 2. The U+2028 / U+2029 line separators (which are valid JSON but not
* valid JavaScript string literals) are escaped.
*/
declare function serializeSnapshot(snapshot: Record<string, unknown>, options?: {
/**
* Custom replacer passed through to `JSON.stringify`.
*/
replacer?: (key: string, value: unknown) => unknown;
/**
* Pretty-printing indent. Defaults to 0 (compact).
*/
space?: number;
}): string;
/**
* Convenience that snapshots a store and serializes the result in one call.
*
* const payload = serializeStore(serverStore);
* // payload is a script-safe JSON string
*/
declare function serializeStore(store: AtomStore, options?: Parameters<typeof serializeSnapshot>[1]): string;
type HydrateAtomsScriptProps = {
/**
* The serialized snapshot to embed. Pass either a pre-serialized string
* (from {@link serializeStore}) or a raw snapshot object — the component
* will serialize it for you.
*/
snapshot: Record<string, unknown> | string;
/**
* DOM id for the `<script>` element. Defaults to
* {@link DEFAULT_HYDRATION_SCRIPT_ID}. Set this when embedding multiple
* stores in one document (e.g. a shell + a route-level boundary).
*/
id?: string;
/**
* `nonce` for CSP-protected pages.
*/
nonce?: string;
};
/**
* Renders an inline `<script type="application/json">` carrying a store
* snapshot for the client to pick up.
*
* Place this once per `<AtomStoreProvider>` you want to hydrate. The
* matching client-side call is {@link readHydration}.
*
* // server
* <AtomStoreProvider store={serverStore}>
* <App />
* <HydrateAtomsScript snapshot={serverStore.snapshot()} />
* </AtomStoreProvider>
*
* // client root
* <AtomStoreProvider initialValues={readHydration() ?? undefined}>
* <App />
* </AtomStoreProvider>
*/
declare function HydrateAtomsScript({
snapshot,
id,
nonce
}: HydrateAtomsScriptProps): React.JSX.Element;
/**
* Read a hydration snapshot embedded via {@link HydrateAtomsScript} from
* the current document.
*
* - On the server (no `document`), returns `null`.
* - When the script tag is missing, returns `null`.
* - When the script body is not valid JSON, returns `null` and logs the
* error via `console.error` (so a malformed payload is visible during
* development but does not crash hydration).
*/
declare function readHydration(id?: string): Record<string, unknown> | null;
//#endregion
export { DEFAULT_HYDRATION_SCRIPT_ID, HydrateAtomsScript, HydrateAtomsScriptProps, readHydration, serializeSnapshot, serializeStore };
//# sourceMappingURL=ssr.d.mts.map