Skip to Content
ohooks

useMap

Manages a reactive Map state with immutable helper actions for setting, deleting, clearing, and querying entries.

Loading preview...

The useMap hook manages a JavaScript Map in React state and provides memoized helper methods (set, setAll, remove, clear, reset, and get) that clone the map on every mutation to trigger re-renders cleanly.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useMap.json

Usage

Examples

Adding and Removing Key-Value Entries

Use set, remove, and clear to update entries reactively without mutating the underlying Map instance in place.

API Reference

UseMapActions

Memoized map mutation and lookup methods returned as the second tuple element.

Property
Specification
set
function
Sets or updates the value for key
setAll
function
Replaces the entire map with the provided entries
remove
function
Deletes key from the map if present
clear
function
Removes all entries from the map
reset
function
Resets the map back to initialValue
get
function
Returns the value associated with key, or undefined
has
function
Returns true if key exists in the map