hookstate
hookstate copied to clipboard
Add Subscribable extenstion (WAS: Add a feature to subscribe for changes in state from outside any component)
I want to perform some business logic (nothing ui related) when a state changes. Right now only way to subscribe is to do it inside a component using the useHookstate hook. I believe we should have a way to subscribe outside components also.
If you have used stores in Svelte, they allow us to subscribe to the store from anywhere.
Please read the docs carefully. Especially sections 'global state' and 'writing a plugin' which describe what you want to achieve.
On Sat, 30 Apr 2022, 21:10 Natesh Bhat, @.***> wrote:
I want to perform some business logic (nothing ui related) when a state changes. Right now only way to subscribe is to do it inside a component using the useHookstate hook. I believe we should have a way to subscribe outside components also.
If you have used stores in Svelte, they allow us to subscribe to the store from anywhere.
— Reply to this email directly, view it on GitHub https://github.com/avkonst/hookstate/issues/282, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6JSVIPCNGIF45WLVKVPE3VHT2GPANCNFSM5UYCTJZQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>
@avkonst the documentation says that :
The state is created by createState. The first argument is the initial state value. The result value is an instance of State, which can be used directly to get and set the state value outside of a React component.
What i want is not to just get or set the state from outside a component. What i want is a way to subscribe to changes in a state (or nested state) so that every time when that state gets updated, my callback function could be called.
This is similar to store subscription in svelte which is extremely useful.
Here's an example scenario. Whenever the app language changes in the global app state, i want to push the new app language value to a backend service. This kind of use case is common and it doesn't need someone to design and implement a plugin to be able to do this. (writing a plugin is considered as advanced usage and for this simple usecase, it doesn't need to require a plugin to be implemented imo)
Example api usage :
myappstate.language.subscribe( (value) => console.log('new app language is : ' , value) );
New extension will be added to Hookstate-4
the extension has been implemented for Hookstate-4. Here is how to use it: https://hookstate.js.org/docs/extensions-subscribable
@avkonst The example in the documentation doesn't show how to subscribe for the state from outside the component and looks like ur passing the subscribable extention into the usehookstate hook which again can only be used inside of a component.
Let's say I have a service class that wants to subscribe for global state changes. Since its not a component I can't do it right now.
On Sat, 6 Aug, 2022, 4:41 pm Andrey, @.***> wrote:
Closed #282 https://github.com/avkonst/hookstate/issues/282 as completed.
— Reply to this email directly, view it on GitHub https://github.com/avkonst/hookstate/issues/282#event-7140836218, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFRTSNTPDR73BI4QIR7Y2MLVXZB5DANCNFSM5UYCTJZQ . You are receiving this because you authored the thread.Message ID: @.***>
Call 'hookstate' function instead of useHookstate. It has the same signature and features and can be called to create states from outside of components.
On Sun, 7 Aug 2022, 00:32 Natesh Bhat, @.***> wrote:
The example in the documentation doesn't show how to subscribe for the state from outside the component and looks like ur passing the subscribable extention into the usehookstate hook which again can only be used inside of a component.
Let's say I have a service class that wants to subscribe for global state changes. Since its not a component I can't do it right now.
On Sat, 6 Aug, 2022, 4:41 pm Andrey, @.***> wrote:
Closed #282 https://github.com/avkonst/hookstate/issues/282 as completed.
— Reply to this email directly, view it on GitHub https://github.com/avkonst/hookstate/issues/282#event-7140836218, or unsubscribe < https://github.com/notifications/unsubscribe-auth/AFRTSNTPDR73BI4QIR7Y2MLVXZB5DANCNFSM5UYCTJZQ
. You are receiving this because you authored the thread.Message ID: @.***>
— Reply to this email directly, view it on GitHub https://github.com/avkonst/hookstate/issues/282#issuecomment-1207206657, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6JSVLH6SJMJZ53TXUUJPTVXZLL7ANCNFSM5UYCTJZQ . You are receiving this because you modified the open/close state.Message ID: @.***>