1.0.6 • Published 9 years ago
redux-connect-actions v1.0.6
redux-connect-actions
Redux helper to handle actions
Install
$ npm i --save redux-connect-actions
Usage
//constants.js
const constants = {
ADD_TODO: 'ADD_TODO',
SET_VISIBILITY_FILTER: 'SET_VISIBILITY_FILTER',
}
//actions.js
import connectActions from 'redux-connect-actions'
import constants from './constants'
function addTodo(state, action){
return Object.assign({}, state, {
todos: [...state.todos, {
text: action.payload,
completed: false
}]
})
}
function setVisibilityFilter(state, action){
return Object.assign({}, state, {
visibilityFilter: action.filter
})
}
export default connectActions({
[constants.ADD_TODO]: addTodo,
[constants.SET_VISIBILITY_FILTER]: setVisibilityFilter
})
//index.js
function todoApp = createStore(
connectActions(actionMap, initialState)
)
API
connectionActions(actionMap [, initialState [, defaultFn]])
actionMap
- an object which maps anaction.type
to a function to handle that action. Whenever an action with a specifictype
goes through redux, if a key with that name is found in theactionMap
object, the corresponding function is called, since it's assigned to handle that action.initialState
- optionaldefaultFn
- optional. If no matching function found, thedefaultFn
will be called