Last active
November 14, 2016 16:10
-
-
Save a-eid/f699da9d0630b2e4c6993938f5818c6c to your computer and use it in GitHub Desktop.
simple reducer decomposition
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
const todos(state=[] , action){ | |
switch(action.type){ | |
case 'add_todo': | |
case 'remove_todo': | |
case 'toggle_todo': | |
return todo(state , action) | |
default : | |
return state | |
} | |
} | |
const todo = function(state = {} , action){ | |
switch(action.type){ | |
case 'add_todo': | |
return [...state , { | |
action.id , | |
action.text , | |
completed : false | |
}] | |
case 'remove_todo': | |
return [...state.slice(0 , action.id) , ...state( action.id + 1)] | |
case 'toggle_todo': | |
return [ | |
...state.slice(0 , action.id) , | |
{...state[action.id] , completed : !state[action.id].completed }, | |
...state.slice(action.id + 1) | |
] | |
} | |
} | |
// visibility_filter -> (show_completed , show_active , show_all); | |
const visibility_filter(state = "show_all" , action){ | |
switch(action.type){ | |
case "show_all": | |
return "show_all" | |
case "show_completed": | |
return "show_completed" | |
case "show_active": | |
return "show_active" | |
default : | |
return state ; | |
} | |
} | |
// simple reducer decompossion . | |
// { | |
// todos : [] , | |
// visibility_filter : "show_all" | |
// } | |
// main reducer : could use combineReducer function . | |
const reducer = (state = {} , action){ | |
return { | |
todos : todos(state.todos , action) , | |
visibility_filter : visibility_filter(state.visibility_filter , action) | |
} | |
} | |
// combineReducers | |
const {combineReducers} = Redux | |
const reducer2 = combineReducers({ | |
todos, | |
visibility_filter | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment