{"action":"created","issue":{"url":"https://api.github.com/repos/reactjs/redux/issues/2017","repository_url":"https://api.github.com/repos/reactjs/redux","labels_url":"https://api.github.com/repos/reactjs/redux/issues/2017/labels{/name}","comments_url":"https://api.github.com/repos/reactjs/redux/issues/2017/comments","events_url":"https://api.github.com/repos/reactjs/redux/issues/2017/events","html_url":"https://github.com/reactjs/redux/issues/2017","id":181196879,"number":2017,"title":"replaceReducer safely","user":{"login":"davidnormo","id":881849,"avatar_url":"https://avatars.githubusercontent.com/u/881849?v=3","gravatar_id":"","url":"https://api.github.com/users/davidnormo","html_url":"https://github.com/davidnormo","followers_url":"https://api.github.com/users/davidnormo/followers","following_url":"https://api.github.com/users/davidnormo/following{/other_user}","gists_url":"https://api.github.com/users/davidnormo/gists{/gist_id}","starred_url":"https://api.github.com/users/davidnormo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidnormo/subscriptions","organizations_url":"https://api.github.com/users/davidnormo/orgs","repos_url":"https://api.github.com/users/davidnormo/repos","events_url":"https://api.github.com/users/davidnormo/events{/privacy}","received_events_url":"https://api.github.com/users/davidnormo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-10-05T16:01:46Z","updated_at":"2016-10-05T16:43:57Z","closed_at":null,"body":"I am thinking of using `store.replaceReducer` to swap reducers  so that an action type can target a particular reducer. This pattern would help with reusing action types on different pages of an application. \r\n\r\nFor example: \r\n1. on /pageA we have reducer A and the following state tree: `{ A: { ... } }`\r\n2. `MY_ACTION` is dispatched and handled by A and produces a new state A.\r\n3. User goes to /pageB, we replaces reducer A with reducer B and end up with `{ A: { ... }, B: { ... } }`\r\n4. `MY_ACTION` is dispatched that B handles and updates state B but not state A.\r\n\r\nThe problem with the above approach is that Redux does not currently provide any safety while replacing a reducer. For example the following scenario is a problem:\r\n\r\n```\r\nfetch().then(dispatch(action));\r\n\r\nstore.replaceReducer(B);\r\n\r\n// dispatch happens by might not have been intended for reducer B - a race condition!\r\n```\r\n\r\nIs there something I'm missing here? Or would you be willing to consider a PR to make this possible?"},"comment":{"url":"https://api.github.com/repos/reactjs/redux/issues/comments/251730280","html_url":"https://github.com/reactjs/redux/issues/2017#issuecomment-251730280","issue_url":"https://api.github.com/repos/reactjs/redux/issues/2017","id":251730280,"user":{"login":"davidnormo","id":881849,"avatar_url":"https://avatars.githubusercontent.com/u/881849?v=3","gravatar_id":"","url":"https://api.github.com/users/davidnormo","html_url":"https://github.com/davidnormo","followers_url":"https://api.github.com/users/davidnormo/followers","following_url":"https://api.github.com/users/davidnormo/following{/other_user}","gists_url":"https://api.github.com/users/davidnormo/gists{/gist_id}","starred_url":"https://api.github.com/users/davidnormo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidnormo/subscriptions","organizations_url":"https://api.github.com/users/davidnormo/orgs","repos_url":"https://api.github.com/users/davidnormo/repos","events_url":"https://api.github.com/users/davidnormo/events{/privacy}","received_events_url":"https://api.github.com/users/davidnormo/received_events","type":"User","site_admin":false},"created_at":"2016-10-05T16:43:56Z","updated_at":"2016-10-05T16:43:56Z","body":"Sure, sorry I'll try to clarify. \r\n\r\nIt seems to me that `replaceReducer` has no safety against race conditions from async actions. So the following scenario has a race condition:\r\n\r\n1. A thunk is dispatched, it dispatches an action to say a fetch has started, and then calls `fetch()`\r\n2. For whatever reason the reducer is replaced with a new reducer\r\n3. The response from the server is returned and the thunk dispatches another action to say the server has responded.\r\n4. Problem: The new reducer has been called with the action but the thunk intended for the original reducer alone to be receive it.\r\n\r\nSafety from this kind of scenario is helpful if you want to use `replaceReducer` to swap reducers, for example, when you want a separate reducer for each page of your application.  I understand this isn't the usual approach to target separate reducers.\r\n\r\nDoes that make more sense?\r\nThanks"}}
{"actor":{"display_login":"davidnormo"}}