Skip to content

Instantly share code, notes, and snippets.

@josegl
Last active November 17, 2015 10:47
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 josegl/1c384a1827cba87d17c0 to your computer and use it in GitHub Desktop.
Save josegl/1c384a1827cba87d17c0 to your computer and use it in GitHub Desktop.
Best redux way to extract an item from an array
let action = (id, items) => {
return {
type: ACTION_TYPE;
item: items.filter(i => id === i._id)[0] // item with id will always exists in the array
}
}
let item = (state = initialState, action) => {
switch (action.type){
case ACTION_TYPE:
return Object.assign({}, state, action.item);
// other item properties update action handlers.
}
}
let action = id => {
return {
type: ACTION_TYPE;
item_id: id
}
}
let items = (state = initialState, action) => {
switch(action.type){
// array items handler
}
}
let itemById = (state = initialState, action) => {
switch (action.type){
case ACTION_TYPE:
//get item with id from items reducer somehow.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment