suddenly-redux
v1.3.0
Published
Simple Redux helpers
Downloads
12
Readme
Suddenly Redux
Simple Redux helpers.
npm install --save suddenly-redux
constants
Inside something like thing-actions.js
const { constants } = require('suddenly-redux');
const Immutable = require('immutable');
const Actions = constants([
'ADD_THING'
]);
Actions.addThing = (thing) => {
// ...
};
module.exports = Actions;
queryString
Convert a javascript object to a query string.
const { queryString } = require('suddenly-redux');
function makeURL (query) {
query = queryString(query);
return `/api/v1/things${query}`;
}
makeURL({ name: 'Nathan' }); // /api/v1/things?name=Nathan
makeURL(); // /api/v1/things
reducer
Define a reducer as (Action constant) keyed handlers.
const { reducer } = require('suddenly-redux');
const Immutable = require('immutable');
const Actions = require('../actions/thing-actions');
const initial_state = Immutable.fromJS({
by_id: {}
});
module.exports = reducer(initial_state, {
[Actions.ADD_THING]: (state, action) => {
return state.merge({
by_id: state.get('by_id').set(action.payload.get('id'), action.payload);
});
}
});
combineReducers
This method simply wraps Redux Immutable's combineReducers
;
createStore
Creates a Redux store (with Thunk enabled by default).
const { createStore } = require('suddenly-redux');
const Immutable = require('immutable');
const reducer = require('./reducers');
let initial_state = Immutable.Map({});
if (typeof window !== "undefined" && window.__STATE__) {
initial_state = Immutable.fromJS(window.__STATE__);
}
// Enable Hot reload for the reducer
if (process.env.NODE_ENV !== 'production' && module.hot) {
module.hot.accept('./reducers', () => {
const replacement_reducer = require('./reducers');
store.replaceReducer(replacement_reducer);
});
}
const store = createStore(reducer, initial_state);
module.exports = store;