{"action":"created","issue":{"url":"https://api.github.com/repos/rackt/redux/issues/976","labels_url":"https://api.github.com/repos/rackt/redux/issues/976/labels{/name}","comments_url":"https://api.github.com/repos/rackt/redux/issues/976/comments","events_url":"https://api.github.com/repos/rackt/redux/issues/976/events","html_url":"https://github.com/rackt/redux/issues/976","id":114098180,"number":976,"title":"Using conditions in reducer when updating specific object","user":{"login":"kudlajz","id":4209081,"avatar_url":"https://avatars.githubusercontent.com/u/4209081?v=3","gravatar_id":"","url":"https://api.github.com/users/kudlajz","html_url":"https://github.com/kudlajz","followers_url":"https://api.github.com/users/kudlajz/followers","following_url":"https://api.github.com/users/kudlajz/following{/other_user}","gists_url":"https://api.github.com/users/kudlajz/gists{/gist_id}","starred_url":"https://api.github.com/users/kudlajz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kudlajz/subscriptions","organizations_url":"https://api.github.com/users/kudlajz/orgs","repos_url":"https://api.github.com/users/kudlajz/repos","events_url":"https://api.github.com/users/kudlajz/events{/privacy}","received_events_url":"https://api.github.com/users/kudlajz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-29T17:02:29Z","updated_at":"2015-10-29T17:13:31Z","closed_at":null,"body":"I have a following snippet of code:\r\n```\r\nconst defaultState = {\r\n\ttraded: {\r\n\t\tactiveColumn: 'update',\r\n\t\torder: 'DESC'\r\n\t},\r\n\tperspective: {\r\n\t\tactiveColumn: 'created',\r\n\t\torder: 'DESC'\r\n\t}\r\n};\r\n\r\nfunction gridReduce(state, action) {\r\n\tswitch (action.type) {\r\n\t\tcase GridActions.GRID_SELECT_COLUMN:\r\n\t\t\treturn Object.assign({}, ...state, {\r\n\t\t\t\tactiveColumn: action.column,\r\n\t\t\t\torder: action.order\r\n\t\t\t});\r\n\t\tdefault:\r\n\t\t\treturn state;\r\n\t}\r\n}\r\n\r\nexport default function (state = defaultState, action = {}) {\r\n\tlet newState = {};\r\n\tfor (let grid in state) {\r\n\t\tif (state.hasOwnProperty(grid)) {\r\n\t\t\tif (action.grid === grid) {\r\n\t\t\t\tnewState[grid] = Object.assign({}, gridReduce(state[grid], action));\r\n\t\t\t} else {\r\n\t\t\t\tnewState[grid] = Object.assign({}, state[grid]);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\treturn newState;\r\n}\r\n```\r\n\r\nand my question is: Is it okay to have that condition in my reducer, if I want to change only one object in the state?\r\n\r\nObviously, without the condition, all my objects get updated, which is not desired result."},"comment":{"url":"https://api.github.com/repos/rackt/redux/issues/comments/152252860","html_url":"https://github.com/rackt/redux/issues/976#issuecomment-152252860","issue_url":"https://api.github.com/repos/rackt/redux/issues/976","id":152252860,"user":{"login":"gaearon","id":810438,"avatar_url":"https://avatars.githubusercontent.com/u/810438?v=3","gravatar_id":"","url":"https://api.github.com/users/gaearon","html_url":"https://github.com/gaearon","followers_url":"https://api.github.com/users/gaearon/followers","following_url":"https://api.github.com/users/gaearon/following{/other_user}","gists_url":"https://api.github.com/users/gaearon/gists{/gist_id}","starred_url":"https://api.github.com/users/gaearon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gaearon/subscriptions","organizations_url":"https://api.github.com/users/gaearon/orgs","repos_url":"https://api.github.com/users/gaearon/repos","events_url":"https://api.github.com/users/gaearon/events{/privacy}","received_events_url":"https://api.github.com/users/gaearon/received_events","type":"User","site_admin":false},"created_at":"2015-10-29T17:13:31Z","updated_at":"2015-10-29T17:13:31Z","body":"Don't pass objects from state in actions if possible. It's almost always an anti pattern.\r\n\r\nPass the *information* to identify the appropriate object. For example you can keep \"grids\" by ID and pass an ID in the action. Then you won't need a for loop."}}