Skip to content

Instantly share code, notes, and snippets.

@tclain
Created June 14, 2016 08:39
Show Gist options
  • Save tclain/9c621bf0bb0ccf245d8577a88f122212 to your computer and use it in GitHub Desktop.
Save tclain/9c621bf0bb0ccf245d8577a88f122212 to your computer and use it in GitHub Desktop.
Redux CheatSheet
export const LIST_ALL_ITEMS = "LIST_ALL_ITEMS";
export function listAllItems(page = 0, limit = 30){
return {
type : LIST_ALL_ITEMS,
page,
limit
}
}
export const CREATE_ITEM = "CREATE_ITEM";
export function create(payload){
return {
type : LIST_ALL_ITEMS,
payload
}
}
// app.jsx
import React from 'react';
import {render} from 'react-dom';
import {hashHistory, Route, IndexRoute, Router} from 'react-router';
import {Provider} from 'react-redux';
import {createStore} from 'redux';
import {syncHistoryWithStore} from 'react-router-redux';
import reducers from './redux/reducers';
let store = createStore(reducers, {initialStateItem: "value"}});
const history = syncHistoryWithStore(hashHistory, store);
render(
<Provider store={store}>
<Router history={hashHistory}>
<Route path="/" component={App}>
</Route>
</Router>
</Provider>
, document.getElementById('app'));
export items = (state, action) => {
switch(action.type){
case "CREATE_ITEM" :
// clone items array
let items = state.items.slice(0);
// add a new item
items.push(action.payload);
// return a swallow copy of state -- see immutability principles
return Object.assign({}, state)
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment