barren
A lightweight state management for React
install
npm i barren
usage
On the main/entry React component
import StoreProvider from 'barren/lib/provider' import createStore from 'barren/lib/createStore' // should be imported elsewhereconst ACTION_STORE = { ifparam === 'add' thisSTORE'counter' += 1 ifparam === 'reduce' thisSTORE'counter' -= 1 } // should be imported elsewhereconst STORE = counter: 0; const barren = ReactDOM
On children component
import store from 'barren' { const store = props const count = store { store } { store } return <div => <button => - </button> count <button => + </button> </div> ;} App
async
Usage sample using axios
const ACTION_STORE = { const users = 'tom' repos = 1 followers = 1 } = params const query = `users?q=+repos:>+followers:>` return } const STORE = // to process output from ACTION_STORE use function { const total_count = 0 items = } = result return usersCount: total_count userList: items }
On children component
import store from 'barren' { const store = props const userCount = store const userList = store const input setInput = const onChange = { } { store } return <div> <input =/> <button =>search github users</button> <p>users count: userCount</p> <pre> userList && userListlength && JSON </pre> </div> } App
api
store
dispatch an action from ACTION_STORE
- action: (object) A valid string identifier, should be unique.
- params: (any types) Parameter to pass to the dispatcher event reference.
- options: (object, optional) Options parameter for the dispatcher.
- caller: (function, optional) Callback function to run after the dispatcher event finish.
options parameter
- forceRewrite (boolean) Force rewriting the store cache on request.
- shallowUpdate (boolean) Once request is made do not cache to the store.
- timeout (integer) Custom timeout for for dispatcher request.
- event (boolean) Dispatch as event to listeners.
- inflightRequestCancellation (boolean) Assign request cancellation handler to the dispatcher.
store
fetch result from STORE
- action: (string) A valid string identifier, should be unique.
- param: (string, optional) Property name of the result if is a valid object.
store
flag an action to process next request
- action: (string) A valid string identifier, should be unique.
store
flag all actions to process all incoming request
store
override the store cache value
- action: (string) A valid string identifier, should be unique.
- value: (any types) New value for the reassignment.
forcefully emit an event
- event: (string) A valid string identifier, should be unique.
- params: (any types) Value to pass to the event.