import { simpleActions } from 'redux-pirate-actions';
export const createDependencies = (actions, store) => {
const actionsCallbacks = bindActionCreators(simpleActions(actions), store.dispatch)
return [store, actionsCallbacks]
}
Now you doesn't need any type constants for redux. The type field will auto generate with redux-pirate-actions
using the function name for that.
Actions can be any simple functions
// simple promise
const foo = () => new Promise();
// an object
const boo = () => ({ data: { field: 1, field2: 2 }});
// or almost nothing, but this way you will see the warn in the console with message: 'The action goo should return Object or Promise';
const goo = () => {};
npm install redux-pirate-actions -s
We trap to constants hell quite often using pure redux functions. And often the constant have same name with action.
const CHANGE_SOMETHING = 'CHANGE_SOMETHING';
const changeSomething = ()=> ({
type: CHANGE_SOMETHING
});
The code become a rubbish, and it can be better. So we drop the constatns and create redux-pirate-actions
to automate this process.
There few way to use the redux-pirate-actions
. We provide two defined methods.
simpleActions
- the actions wrapper that define the type filed when it doesn't existssetActionTypes
- the way to setup types object to be able use it in the reducersgetTypes
- the way to get the types object at the reducers
NOTE: to simplify the usage we set the copy of the types object to the window.types
object. But it's mostly lifehack here.
First of all we needs some actions.
export const foo = () => ({ data: 'foo'});
export const boo = () => ({ data: 'foo'});
Then we have to wrap actions before use bindActionCreators
funcion from redux
.
import { bindActionCreators } from 'redux'
import { simpleActions } from 'redux-pirate-actions';
export default (actions, store) => {
const actionsCallbacks = bindActionCreators(simpleActions(actions), store.dispatch)
return [store, actionsCallbacks]
And now at reducers we use actions by name
import { getTypes } from 'redux-pirate-actions';
const types = getTypes();
// or types can come from window object
export function someData(state = '', action) {
switch (action.type) {
case types.foo:
return action.data;
case types.boo:
return action.data;
default:
return state
}
}
The types object should be received before redux
init the store with default state. Other vise types would be empty.
We provide possibility to generate types list before simpleActions
will call via the setActionTypes
.
import { setActionTypes } from 'redux-pirate-actions';
...
setActionTypes(actions);
...
export const store = createStore({}, reducers);
This module was developed to act with redux-pirate-promise together.
- the minimal store should looks like: how to create store
- the minimal controller should looks like: how to use
Copyright (c) 2017 Pirate Minds. Licensed with The MIT License (MIT).