react-stores
Shared states for React.js (a flux-way shared stores without actions and dispatchers).
How to install
yarn add react-stores
or npm i react-stores --save
Demo
- Clone this repo
cd
into ityarn install
ornpm i
npm run demo
localhost:9000
in your browser
Tests
npm run test
How to use
First you need to create a Store
singleton
Then you need to create a StoreComponent
that will use store singleton
v1.2.0 Event driven component–store connection
v1.3.0 Decorator for make React Component React Sores driven. You can use multiple
Now you can use it as usual
You can mutate states from any point of your app like this
...or even like a flux actions
Also you can get store state values from everywhere in your app
API
StoreComponent lyfecycle
Store
StoreEvent
StoreEventType
'all' // fires with every other events (init or update)
'init' // fires once at as soon as event has bound
'update' // fires at each store update
'dumpUpdated' // fires at each dump update
Persistence
// LocalStorage
export let store: Store<State> = new Store<State>(initialState, new StorePersistentLocalSrorageDriver('comon'));
Persistent driver
abstract class StorePersistantDriver<StoreState> {
constructor(readonly name: string) {}
public abstract write(state: StoreState): void;
public abstract read(): StoreState;
}