Skip to main content
Module

x/valtio/docs/api/utils/proxyMap.mdx

πŸ’Š Valtio makes proxy-state simple for React and Vanilla
Go to Latest
File
---title: 'proxyMap'section: 'API'subSection: 'Utils'description: ''---
# proxyMap
#### use a js Map with Valtio
This utility creates a proxy which mimics the native Map behavior. The API is the same as the Map API.
```jsimport { proxyMap } from 'valtio/utils'
const state = proxyMap()state.size // ---> 0
state.set(1, 'hello')state.size // ---> 1
state.delete(1)state.size // ---> 0```
#### Nesting
It can be used inside a `proxy` as well.
```jsimport { proxyMap } from 'valtio/utils'
const state = proxy({ count: 1, map: proxyMap(),})```
When using an object as a key, you can wrap it with `ref` so it's not proxied. This is useful if you want to preserve the key equality
```jsimport { proxyMap } from 'valtio/utils'
// with refconst key = ref({})state.set(key, 'hello')state.get(key) //hello
// without refconst key = {}state.set(key, 'value')state.get(key) //undefined```
## Codesandbox demo
https://codesandbox.io/s/github/pmndrs/valtio/tree/main/examples/todo-with-proxyMap