Closed christopherjbaker closed 1 year ago
@christopherjbaker after reading https://overreacted.io/why-isnt-x-a-hook/ I am not sure our API choice is optimal.
Namely, the timing of when you call the useStore()
matters, because it sets up listeners, so read before it aren't registered and opening a new observer closes the last one, which may break composition.
What are your thoughts?
TLDR: We are providing something that sits outside the norm of React, but I don't think we violate anything brought up in that article.
@BigAB That was a good read. I don't think we violate the issues brought up in that article though. useStore
and useObserver
do not break composition. I think it is perfectly reasonable that you cannot use observables until after you declare that you are going to, in the same way that you cannot use state until after you declare said state. You can absolutely useStore
multiple times (this will create multiple observation readers under the hood, but that will not cause any issues), and debugging does not become any more complicated (other than what observables themselves add).
This is an attempt at #191. The old code is still in place, though it has been moved to the
legacy
folder.Usage
useStore
The primary use. Pass a
Store
constructor and props, returns and observable to use in the render.useObserver
An alternative use when you have an existing store and just need observability.
ModelProvider
Though the idea is not hook-specific, we have been discussing this for quite some time, so I decided to include it in the PR. Allows you to provide model classes at higher levels and consume them lower. Searches cascade up the tree.