fluxible icon indicating copy to clipboard operation
fluxible copied to clipboard

feat: draft implementation of useStore in #733 (not production ready)

Open billyrrr opened this issue 3 years ago • 4 comments

Co-authored-by: pablopalacios [email protected]

I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.

billyrrr avatar Oct 07 '21 08:10 billyrrr

Cool, let's go step by step. Important tests that we should have:

  • It registers/unregsiters to the given store
  • It updates when the store is updated

You can take a look at connectToStores tests as reference. As I mentioned in the issue, one thing that I'm concerned is the unregister part. We must make sure that when the component unmounts, we are removing the listener, otherwise we will have a memory leak.

pablopalacios avatar Oct 09 '21 16:10 pablopalacios

@billyrrr @pablopalacios Still interested in this feature?

redonkulus avatar Sep 06 '22 15:09 redonkulus

@redonkulus I do. At work we are using 2 custom hooks for stores: useStore and useStoreSubscription. The difference is that the later one subscribes to changes for the given store while the first not. We normally use the first when we are sure that the data is static (normally server side data). If this difference is not confusing you, I would propose to include both of them in the API (or, if you have a better idea...).

This is an example on how we use it:

function MyComponent() {
    const { locale } = useStore(AppStore).getState();
    const { count } = useStoreSubscription(CountStore).getState();

    return <span>Count: {new Intl.NumberFormat(locale).format(count)}</span>;
}

We have been using these hooks in production successfully since probably the time this PR was created. @billyrrr do you still have interested in moving the PR forward, otherwise I can prepare another one.

pablopalacios avatar Sep 06 '22 17:09 pablopalacios

Yep, I can continue to work on this issue.

billyrrr avatar Sep 15 '22 04:09 billyrrr