-
-
Notifications
You must be signed in to change notification settings - Fork 20
useStoreListener hook #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,3 +32,31 @@ export function useStore(store, opts = {}) { | |
|
|
||
| return store.get() | ||
| } | ||
|
|
||
| export function useStoreListener(store, opts = {}) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is worth moving the new hook into a separate file: this way it will not affect those who do not need it. |
||
| if (process.env.NODE_ENV !== 'production') { | ||
| if (typeof store === 'function') { | ||
| throw new Error( | ||
| 'Use useStore(Template(id)) or useSync() ' + | ||
| 'from @logux/client/react for templates' | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| let listenerRef = React.useRef(opts.listener) | ||
| listenerRef.current = opts.listener | ||
|
|
||
| React.useEffect(() => { | ||
| let listener = (value, changed) => listenerRef.current(value, changed) | ||
| if (opts.leading) { | ||
| listener(store.get()) | ||
| } | ||
| if (opts.keys) { | ||
| return listenKeys(store, opts.keys, listener) | ||
| } else { | ||
| return store.listen(listener) | ||
| } | ||
| }, [store, '' + opts.keys, opts.leading]) | ||
|
|
||
| return null | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the return does nothing, it seems it can be removed. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this parameter only have a value if the store is
mapstore?And what is its value if the store value is a primitive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap
In this case you do not have
store.setKeyand we have no easy way to detect changesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I was asking because of this: https://github.com/nanostores/react/pull/2/files#r744618456