smitty
Tiny flux implementation built on mitt
smitty
- Install
- Basic Usage
- Demos
- Usage with Preact and React
- API
- Store
- Action Creator Detailed Example
- Class As Reducer
Install
npm install -S smitty
Basic Usage
// Create a store with initial stateconst initialState = count: 0 const store = store // add a reducerstore storeactions console // logs `{ count: 5 }`
Demos (v2)
- Photo Booth Demonstrates async api and saving parts of the store with localforage
Demos (v1)
- Basic
- Async
- Fun
Usage with Preact and React
-
Preact bindings - preact-smitty
npm install preact-smitty -
React bindings - react-smitty
npm install react-smitty
API
createStore(initialState: any)
Arguments
initialState: any
required: Determines the shape and initial state of your store. Can be of any type that you choose.
Returns
Store: Store
Store
Store
emit: (function)
arguments
type: (string | function)
-
[string],
type
determines which reducers are called.const store =storeconsole // logs 0storeconsole // logs 1 -
[function]
type
becomes an action creator that is passed 1 argument- store: Store
This is useful to emit multiple actions from a single emit call.
const store =store{console // logs 1console // logs 2}```
payload: (any) optional
payload to pass to your reducer
const store = storeconsole // logs { name: 'Arrow' }storeconsole // logs { name: 'River' }
createActions(): (function)
arguments
actionMap: (object)
Object where key is the action creator's name and the value can be of type string
or function
.
If the value is a string
, an action creator is attached to store.actions
as a function that accepts one argument, payload
.
store // The following are functionally equivalentstoreactions store
Action creators with a string value can be used as the key in your actionMap
in handleActions
.
store // add a reducerstore storeactions console // logs `{ count: 5 }`
If the value is a function
, it must be a function that returns an action creator. For async action creators.
store storeactions
handleActions(): (function)
arguments
handlerMap: (object)
Object with keys that correspond to action types passed to emit
When an event is emitted and the key matches the type the reducer is invoked with 3 arguments.
- state: (any) the store's state getter
- payload (any) the payload that was emitted
- type (string) the type that was emitted
const store = storeconsole // logs { color: 'blue', hovered: false }storeconsole // { color: 'red', hovered: false }storeconsole // { color: 'green', hovered: true, highlighted: false
actions: (object)
Map of all the actions created in store.createActions
This is convenient so that you do not have to deal with action imports across your app.
on: (function)
Convenience shortcut for mitt.on.
off: (function)
Convenience shortcut for mitt.off.
Action Creator Detailed Example
You can pass a function to emit
in order to create an action creator
// Create a store with initial stateconst initialState = {}const store = // add our reducerstore // create our action creatorsconst actions = { return async { const res = await window resdata = await res } } // When calling emit with a function argument, the function will be called with `emit` and `state` as argumentsconst result = store // Return whatever you like from your action creatorconsole // logs "Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}" // After the fetch call, `REQUEST_ROOM` is fired a second time with our response dataresult // logs `{ 1a: { id: '1a', title: 'My Room' }``
Class As Reducer
Reducers are iterated with for (let type in reducer) {...}
with no obj.hasOwnProperty
check so this works.
const store = { thishistory = thishistory } { thishistory } HistoryReducerprototype'foo/ADD' = { statefoo += efoo this} const historyReducer = store storeconsole // logs 10storeconsole // logs 17console// logs// [// { state: { foo: 10 }, e: { foo: 5 }, type: 'foo/ADD' },// { state: { foo: 17 }, e: { foo: 7 }, type: 'foo/ADD' }// ]