Last active
November 17, 2015 10:47
-
-
Save josegl/1c384a1827cba87d17c0 to your computer and use it in GitHub Desktop.
Best redux way to extract an item from an array
This file contains 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
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 | |
} | |
} |
This file contains 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
let item = (state = initialState, action) => { | |
switch (action.type){ | |
case ACTION_TYPE: | |
return Object.assign({}, state, action.item); | |
// other item properties update action handlers. | |
} | |
} |
This file contains 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
let action = id => { | |
return { | |
type: ACTION_TYPE; | |
item_id: id | |
} | |
} |
This file contains 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
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