Skip to content

Pass the whole action to caseReducer #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ Example usage:
```js
import {createReducer} from "@acemarke/redux-starter-kit";

function addTodo(state, newTodo) {
function addTodo(state, action) {
const {newTodo} = action.payload;

// Can safely call state.push() here
state.push({...newTodo, completed : false});
}

function toggleTodo(state, payload) {
const {index} = payload;
function toggleTodo(state, action) {
const {index} = action.payload;

const todo = state[index];
// Can directly modify the todo object
Expand Down
8 changes: 3 additions & 5 deletions src/createReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import createNextState from "immer";

export function createReducer(initialState, actionsMap) {
return function(state = initialState, action) {
const {type, payload} = action;

return createNextState(state, draft => {
const caseReducer = actionsMap[type];
const caseReducer = actionsMap[action.type];

if(caseReducer) {
return caseReducer(draft, payload);
return caseReducer(draft, action);
}

return draft;
});
}
}
}