valtio
Version:
🧙 Valtio makes proxy-state simple for React and Vanilla
22 lines (21 loc) • 851 B
TypeScript
import { useSnapshot } from 'valtio/react';
/**
* useProxy
*
* Takes a proxy and returns a new proxy which you can use in both react render
* and in callbacks. The root reference is replaced on every render, but the
* keys (and subkeys) below it are stable until they're intentionally mutated.
* For the best ergonomics, you can export a custom hook from your store, so you
* don't have to figure out a separate name for the hook reference. E.g.:
*
* @example
* export const store = proxy(initialState)
* export const useStore = () => useProxy(store)
* // in the component file:
* function Cmp() {
* const store = useStore()
* return <button onClick={() => {store.count++}}>{store.count}</button>
* }
*
*/
export declare function useProxy<T extends object>(proxy: T, options?: NonNullable<Parameters<typeof useSnapshot>[1]>): T;