React xStore
Simple yet powerful global state management for React, Inspired by Vuex, Redux and react-global-store, Support's Async mutations by (Actions), see examples for more informations.
Installation
npm install --save react-xstore
Usage
Wrap your components with Connect
and the store will be available as store
in props this.props.store.myStore
then you can access store api from any component you wrap with.
- access store
let mys = this.props.store.myStore
. - get current store state
mys.state.variable
. - call mutations
mys.commit('mutation', ...params)
. - call actions for async code
mys.dispatch('action', ...params)
. - manual state mutation
mys.setState({variable: 'hello'})
. - combine states or shrink object by computed property
mys.computed.variables
. - magic state getter
mys.variable
. - magically update state value by proxy
mys.variable = 'hello'
. - link store with your component, you can add stores as second parameter or leave it blank to init all stores to the component
Connect(Component, ['store1', 'store2'])
.
and you can access the stores externally by import {External as xStore} from 'react-xstore';
then fetch store like xStore.myStore
and now you have all internal methods and states available, you can commit, dispatch, setStore or update state magically.
Example 1 - simple counter
Init Store, you should import it at the main app loader
; name: 'myStore' state: // default state for current store counter: 25 mutations: // called by commit() function { thiscounter = thiscounter ? thiscounter + append : 60; } actions: { // called by dispatch() function ; } computed: { return thiscounter * 5; } ;
access store from any component like this
; ; { const state computed setState commit dispatch = thispropsstoremyStore; return <div> Counter is statecounter and x5 is computedx5<br /> <button onClick= { ; } > setState +1 </button> <button onClick= > Commit +50 </button> <button onClick= > Dispatch Async +100 </button> </div> ; } First 'myStore'; // access wanted stores only, for better performance
Example 2 - advance auth manager
Init Store, and import it at the main app loader
; const LOGIN = 'LOGIN'; const LOGIN_SUCCESS = 'LOGIN_SUCCESS'; const LOGOUT = 'LOGOUT'; name: 'auth' state: isLoggedIn: !!localStorage mutations: { thispending = true; } { thisisLoggedIn = true; thispending = false; } { thisisLoggedIn = false; } actions: { this; return { ; }; } { localStorage; this; } ;
component example usage
; ; { const state dispatch = thispropsstoreauth; return <div> stateisLoggedIn ? <span>Logged</span> : <span>You Need to Login</span> statepending && <span>...</span> <br /> !stateisLoggedIn && <button onClick= > Login </button> stateisLoggedIn && <button onClick= > Logout </button> </div> ; } Login; // access all stores
update store by magic setter
let auth = thispropsstoreauth; authusername = 'Tamer Zorba';
Contributing
Thank you for considering contributing! Fork the repo then after you complete your awesome feature, make a pull request and we will be glad to accept after test.
License
This code is open-sourced software licensed under the MIT license