TrueStore
Dead simple state management on top of Immutable.js.
Install
TrueStore is available on npm:
npm install true-store
Overview
TrueStore provides a simple store with observer capabilities. It also gives you copies of your state when you ask for values, avoiding undesired mutation bugs.
Why you might wanna use it:
- Simplicity: simple
get
andset
interface. No weird magic going on. - Control: Thanks to the explicit observers, you always know when your code will update.
- Performance: Since updates are in your control, if you do it right it will be fast. If you do it wrong, you can fix it. Also, thanks to Immutable.js the observers don't need to run if data updates but is equal to the previous value.
- Predictability: Thanks to Immutable.js you won't run into weird mutation bugs. Your data only updates when you call methods like
set
andmerge
. When reading you'll get copies of your data to do as you please.
constructor
; let store = ; // default state = {}let store = user: name: 'John Doe' ;
get
store; // { user: {name: 'John Doe'} }store; // {name: 'John Doe'}store; // 'John Doe'
set
store;store;store;
merge
store; // { user: { name: 'John Doe', age: 42 }}store;
observer
{} {} {} store;store;store; let observer = store;observer; // observer won't run after release
transaction
// observers will be called only once, after the transaction endsstore; // transactions can be nested, only the root will trigger observersstore;
Integration with React
TrueStore works anywhere. If you wanna use it with React, you just need to:
- Get values from the store and use at will, usually in your
render
method. - Use an observer to tell the component to update when something changes.
- Release the observer when the component unmounts.
Example
store.js
; ;
actions.js
; { let count = store; store;}
counter.js
; ;; Component { thisobserver = store; } { thisobserver; } { return <button onClick=increment> store </button>; }