Package Exports
- send-action
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (send-action) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
send-action
A simple state container.
Install
npm i --save send-actionAbout
send-action is meant to be the smallest, simplest redux-like state management library. The focus is on providing a concise method for triggering actions, and on avoiding complex middleware & development dependencies.
The API is significantly different from redux, but the pattern is similar.
Using send-action you trigger actions, modify state based on those actions, and listen to the changes to render your application.
Minimal example
var sendAction = require('send-action')
/*
* Create send function.
*/
var send = sendAction({
onaction: function (action, state) {
// modify the state based on actions
return state
},
onchange: function (action, state, oldstate) {
// render your application
console.log(action, state, oldstate)
},
state: {}
})
/*
* Send an action to the store
*/
send({ type: 'example' value: 'cool' })
/*
* Alternate `send` syntax
*/
send('example', { value: 'cool' })Example with yo-yo.js
var yo = require('yo-yo')
var sendAction = require('send-action')
/*
* Create send function.
*/
var send = sendAction({
onaction: onaction,
onchange: onchange,
state: {}
})
/*
* Set up the action handler to modify state based on the actions triggered
*/
function onaction (action, state) {
if (action.type === 'example') {
return { value: action.value }
}
}
/*
* Subscribe to changes to the store for rendering & logging
*/
function onchange (action, state, oldState) {
yo.update(document.getElementById('app'), render(state))
}
/*
* Render the html of the app with yo-yo
*/
function render (state) {
yo`<div id="app">heeeeey</div>`
}
document.body.appendChild(render(send.state()))
/*
* Send an action to the store
*/
send({ type: 'example' value: 'cool' })
/*
* Alternate `send` syntax
*/
send('example', { value: 'cool' })