Vize

use-map

Create a reactive Map with typed helpers.

Package @vizejs/composable/use-map
Own the source vize lib pull composable:use-map
Runtime exports useMap
Gzip budget 1280 B

Usage

import { useMap } from "@vizejs/composable/use-map";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
useMap state experimental safe stable none web, server, worker, native, desktop, terminal — —

API

useMap

Create a reactive Map with typed helpers. Key and value types are inferred from the initial entries (useMap([["a", 1]]) is a Map<string, number>); pass type arguments for an initially empty map. Reads through get, has, and map are tracked per key, so an effect reading one key does not re-run when an unrelated key changes. Values are stored as-is (not deeply reactive). Purely synchronous state: safe during server rendering (no host globals, no timers) and nothing to dispose.

function useMap<Key = unknown, Value = unknown>( initial: Iterable<readonly [Key, Value]> = [], ): MapControls<Key, Value>
const counts = useMap<string, number>();
counts.update("clicks", (previous = 0) => previous + 1);

Types

MapControls

Reactive map wrapper returned by useMap.

Member Type Description
map ReadonlyMap<Key, Value> Read-only reactive view of the map. get, has, iteration, and size on this view are tracked per key. Values are stored shallowly.
size ComputedRef<number> Number of entries.
entries ComputedRef<readonly (readonly [Key, Value])[]> Snapshot of the entries in insertion order.
get (key: Key) => Value | undefined Read the value for key. Reactive when read inside an effect.
has (key: Key) => boolean Whether key is present. Reactive when read inside an effect.
set (key: Key, value: Value) => void Store value under key.
delete (key: Key) => boolean Remove key.
clear () => void Remove every entry.
update (key: Key, updater: (previous: Value | undefined) => Value) => Value Replace the value under key with the result of updater.
getOrInsert (key: Key, create: (key: Key) => Value) => Value Return the value under key, inserting create(key) first when absent.
reset () => void Restore the creation-time entries.