jhonnymichel / react-hookstore

A state management library for react using the bleeding edge hooks feature

Home Page:https://codesandbox.io/s/r58pqonkop

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Context vs this?

adjourn opened this issue · comments

Have you ran any benchmarks against context? Or are there any big cons replacing context?

I generally like context (using it right now) but it has to be updated where provider is located which requires state and passing updater method with context as well if I want to update it from anywhere in the app.

It also causes re-render where provider is located (usually app root or big subtree) which brings all the optimization mumbo jumbo with it. This looks like a very appealing alternative due to its simplicity and small package size.

Another things is that context providers and consumers make devtools a nightmare to work with.

Now that I think about the cons, I should reconsider liking context. 😄

I don't see context as a good way to share mutable state, since using providers/consumers is quite verbose and not very versatile (try optimize them, you would end up spreading state instead of having it centralized, so definetely not what one would want).

You could use context to easily provide a store and actions that interact with a store, for example. That's exactly what MobX and Redux do: their Provider component uses context just to provide the store to anyone that needs it, but state management itself is dealt by the stores, and contexts are just used as injectors.

That said, there is nothing wrong with using contexts if you feel confortable with it. But I do believe the React team made it to make it easy to inject stuff on components, but not necessarily to keep changing context. As you said, it is quite hard to optimize re-renders since you don't have MobX inject, Redux connect or state hooks.

About performance, I don't think there will be much difference between react-hookstore and contexts out of the box, but I'll say one thing: It is easey to optimize react-hookstore if you want, because you can easily distribute the store usage to small trees.

But react-hookstore lacks a smart logic to skip updates when they aren't necessary. This is a feature I plan on include in the next release.