Skip to content
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

Merged
merged 3 commits into from Dec 4, 2022

Conversation

jligeza
Copy link
Contributor

@jligeza jligeza commented Dec 4, 2022

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.

@dai-shi
Copy link
Owner

dai-shi commented Dec 4, 2022

Thanks for the suggestion.
This API is entirely based on state keys (and it doesn't assume the internal implementation), so I think subscribe should accept "key".

    subscribe('count', (count) => {
      console.log(`count ${count}`);
    });

Does it solve your use case?

@jligeza
Copy link
Contributor Author

jligeza commented Dec 4, 2022

Yes, it would be good.

@dai-shi
Copy link
Owner

dai-shi commented Dec 4, 2022

Cool. Could you update the PR for it?

@jligeza
Copy link
Contributor Author

jligeza commented Dec 4, 2022

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
Copy link
Owner

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Owner

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

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

LGTM

@dai-shi dai-shi merged commit cf8fd64 into dai-shi:main Dec 4, 2022
@dai-shi
Copy link
Owner

dai-shi commented Dec 4, 2022

Published: https://www.npmjs.com/package/react-hooks-global-state/v/2.1.0
Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants