Created
June 14, 2016 08:39
-
-
Save tclain/9c621bf0bb0ccf245d8577a88f122212 to your computer and use it in GitHub Desktop.
Redux CheatSheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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