Skip to main content
Module

x/valtio/docs/advanced/snapshot.mdx

πŸ’Š Valtio makes proxy-state simple for React and Vanilla
Go to Latest
File
---title: 'snapshot'section: 'Advanced'description: 'Create a snapshot of current state'---
# snapshot
#### A snapshot takes a proxy and returns a immutable object, unwrapped from the proxy.
Immutability is achieved by deeply freezing the object. In sequential snapshot calls, when the values in the proxy have not changed, a pointer to the same object is returned. This allows for shallow comparison in render functions, preventing spurious renders. Snapshots also throw promises, making them work with React Suspense.
```jsimport { proxy, snapshot } from 'valtio'
const store = proxy({ name: 'Mika' })const unwrappedStore = snapshot(store) // unwrapped store is the original object, unproxiedconst anotherUnwrappedStore = snapshot(store)console.log(unwrappedStore === anotherUnwrappedStore) // truestore.name = 'Hanna'const yetAnotherUpwrappedStore = snapshot(store)console.log(unwrappedStore === yetAnotherUpwrappedStore) // false```
#### Vanilla javascript
In VanillaJS, snapshot is not necessary to access proxied object values, inside or outside of subscribe. However, it is useful, for example, to keep a serializable list of unproxied objects or check if objects have changed.
If you are using valtio outside without React, import proxy and snapshot from 'valtio/vanilla'.