@nanostores/react
Advanced tools
Comparing version 0.7.3 to 0.8.0
import type { Store, StoreValue } from 'nanostores' | ||
import type { DependencyList } from 'react' | ||
@@ -9,2 +10,10 @@ type StoreKeys<T> = T extends { setKey: (k: infer K, v: any) => unknown } | ||
/** | ||
* @default | ||
* ```ts | ||
* [store, options.keys] | ||
* ``` | ||
*/ | ||
deps?: DependencyList | ||
/** | ||
* Will re-render components only on specific key changes. | ||
@@ -11,0 +20,0 @@ */ |
23
index.js
import { listenKeys } from 'nanostores' | ||
import { useCallback, useSyncExternalStore } from 'react' | ||
import { useCallback, useRef, useSyncExternalStore } from 'react' | ||
export function useStore(store, opts = {}) { | ||
let emit = (snapshotRef, onChange) => value => { | ||
snapshotRef.current = value | ||
onChange() | ||
} | ||
export function useStore(store, { keys, deps = [store, keys] } = {}) { | ||
let snapshotRef = useRef() | ||
snapshotRef.current = store.get() | ||
let subscribe = useCallback( | ||
onChange => | ||
opts.keys | ||
? listenKeys(store, opts.keys, onChange) | ||
: store.listen(onChange), | ||
[opts.keys, store] | ||
keys?.length > 0 | ||
? listenKeys(store, keys, emit(snapshotRef, onChange)) | ||
: store.listen(emit(snapshotRef, onChange)), | ||
deps | ||
) | ||
let get = () => snapshotRef.current | ||
let get = store.get.bind(store) | ||
return useSyncExternalStore(subscribe, get, get) | ||
} |
{ | ||
"name": "@nanostores/react", | ||
"version": "0.7.3", | ||
"version": "0.8.0", | ||
"description": "React integration for Nano Stores, a tiny state manager with many atomic tree-shakable stores", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4761
64