Skip to content

Instantly share code, notes, and snippets.

@sAbakumoff
Last active November 16, 2016 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sAbakumoff/55ffb611eef5d198b0326372ee6be7e8 to your computer and use it in GitHub Desktop.
Save sAbakumoff/55ffb611eef5d198b0326372ee6be7e8 to your computer and use it in GitHub Desktop.
function addTodo(state, action){
var new_state;
if(action.type !== "ADD_TODO")
return state;
if(!state)
new_state=[];
else
new_state=state.slice();
new_state.push(action.item);
return new_state;
}
var state0 = addTodo(null, {type:"ADD_TODO", item : "abc"});
console.log(state0); //["abc"]
var state1 = addTodo(state0, {type:"ADD_TODO", item : "xyz"});
console.log(state1); //["abc", "xyz"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment