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
expose "subscribe" function for "createGlobalState" #85
Conversation
Thanks for the suggestion. subscribe('count', (count) => {
console.log(`count ${count}`);
}); Does it solve your use case? |
Yes, it would be good. |
Cool. Could you update the PR for it? |
Done. |
@@ -105,6 +105,7 @@ It returns a set of functions | |||
* `useGlobalState`: a custom hook works like React.useState | |||
* `getGlobalState`: a function to get a global state by key outside React | |||
* `setGlobalState`: a function to set a global state by key outside React | |||
* `subscribe`: a function that subscribes to state changes |
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.
The api doc is generated from the source code. Can you edit jsdoc in code and run yarn run apidoc
?
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.
Done.
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.
LGTM
Published: https://www.npmjs.com/package/react-hooks-global-state/v/2.1.0 |
The
subscribe
function would be very helpful, because I often need to persist some parts of global state in local storage, and there is no easy and clean way to do this. Best I could do is to create an effects-only component that listens global state by hooks, which works but is verbose and feels complicated.