Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { listenKeys } from 'nanostores'
import { useCallback, useRef, useSyncExternalStore } from 'react'

let emit = (snapshotRef, onChange) => value => {
Expand All @@ -18,9 +17,16 @@ export function useStore(store, { keys, deps = [store, keys], ssr } = {}) {
let subscribe = useCallback(onChange => {
emit(snapshotRef, onChange)(store.value)

return keys?.length > 0
? listenKeys(store, keys, emit(snapshotRef, onChange))
: store.listen(emit(snapshotRef, onChange))
if (keys && keys.length > 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add it to listenKeys instead to make it possible to use across all tools?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like this idea, please, send PR to nanostores/nanostores (we are going to have a minor release in a few days).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i send a new PR in nanostores/nanostores#408. check it out

let keysSet = new Set(keys).add(undefined)
return store.listen((value, _, changed) => {
let baseKey = changed ? changed.split(/\.|\[/)[0] : undefined
if (keysSet.has(changed) || keysSet.has(baseKey)) {
emit(snapshotRef, onChange)(value)
}
})
}
return store.listen(emit(snapshotRef, onChange))
}, deps)

let get = () => snapshotRef.current
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nanostores/react",
"version": "1.1.0",
"version": "1.1.1",
"description": "React integration for Nano Stores, a tiny state manager with many atomic tree-shakable stores",
"keywords": [
"react",
Expand Down Expand Up @@ -65,7 +65,7 @@
"index.js": "{ useStore }",
"nanostores": "{ map, computed }"
},
"limit": "958 B"
"limit": "977 B"
}
],
"engines": {
Expand Down