what is it
Yet another react state management micro lib.
why
Let's imagine we know nothing of react, and we just got a ticket to share some counter between two components in header and footer, our intuition will tell is something like this:
looking at class above you just immediately get your head around the code because it's pretty straightforward.
And let's imagine that we use Mystate#counter
in several components and wherever we call incrementCounter
we expect our components to update.
like:
although it would not work (but let's imagine it is working), for any programmer the thing above is self explainable, and if one would not even know React, he could read the code and just assume what will happen.
But unfortunately there is no such thing, to implement the minimal sample as above we'll have to do quite a work. Let's say we use redux for that, and for a single property it begins: we want to impress our colleagues don't we?: stores, reducers, hocs, writing hundreds lines, and etc. if we want anything async, or state depending on another state, or some synchronization between them, say goodbye to sanity and wish best of luck to whom will maintain it afterwards. (though in some cases such ceremony is ok and even necessary).
This lib allows you to do pragmatic state management and at the same time allow you to write it in any style you want.
How
just define your state as follows:
to make it work your state class should only do two things:
extend from SubscriptionState
,
in any method you wish subscribed components to update - call this.update()
now lets rewrite the initial example:
and it will just work. Whenever you'll press a button all components that .use()
your myState
, will update.
With minimal intervention, you have explicit, easy to implement and to maintain state. You just write it in standard OOP way.
Recap, you call ${yourStateClassInstance}.use()
in your component, and it will subscribe to the state. Whenever update()
is called in your
state class all the subscribed components will be updated. On unmount, component will be automatically be unsubscribed.
you may pass an instance around or have a singleton for "global" state
optimizations
lib handles the cases for nested subscribed components insuring that they will always be rendered once for each state version. So e.g. if you use context, and some of the child of context user is also uses same context, it be rendered for each time parent rendered, plus own renders, this lib excludes such cases.
for niche cases you can as well pass following options to have fine-grained control over the update
//all options are optional
almost real world example:
in above example, after users loaded it will update both UsersIndex
and UserAddressIndex
immutability
lib doesn't stand in your way and has no opinions how you use it at all.
E.g. it allows to not only have primitive objects in state, but instances of classes and etc.
as well if you want immutability, and e.g. state versioning aka time machine debugging0 :
can i use it in prod?
if you feel adventurous.
lib was used in quite big SPAs with over 9000 hardcore state management related problems, so it's battle tested.
and it just fun to play around with it.
without comments and whitespace it's < 50 lines.
Bonus
if lib looks strange it includes a bonus ProvidableState
which shares almost same interface and behaves almost same,
but uses react's context underhood.
Absolutely no magic, just oopified context usage.
how it works
see SubscriptionState#use() doc comments
licence
MIT