Last active
September 18, 2018 11:36
-
-
Save gastonmorixe/cf6f40578524ddd085dd to your computer and use it in GitHub Desktop.
Redux React CombineReducers Discussion [With Dan Abramov]
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
import { combineReducers } from 'redux' | |
import reduceReducers from 'reduce-reducers' | |
import { invites } from './Invites' | |
import { claimInvite } from './ClaimInvite' | |
import { app } from './App' | |
import * as actions from '../constants/ActionTypes' | |
import {mapping, parseRemotePersonJSON} from './App' | |
const combinedReducer = combineReducers({ | |
invites, | |
app, | |
claimInvite, | |
}) | |
const rootAccessReducer = (state, action) => { | |
switch (action.type) { | |
case `${actions.REQUEST_INVITE}_FULFILLED`: | |
return { ...state, | |
app:{...state.app, | |
loggedPerson: {...state.app.loggedPerson, | |
...parseRemotePersonJSON(action.payload.person), | |
authToken: state.app.loggedPerson.authToken | |
} | |
}, | |
claimInvite:{...state.claimInvite, | |
inviteRequest: {...state.claimInvite.inviteRequest, | |
state: 'loaded' | |
} | |
} | |
} | |
default: | |
return state | |
} | |
} | |
const rootReducer = reduceReducers(combinedReducer, rootAccessReducer) | |
export default rootReducer |
for historical and educational reasons, the happy ending
from dan: @gastn___ I like the last approach you suggested.
I'm currently in the boat you were in during the time of this gist. I was hoping you could help shed some light. How did you handle higher order components/reducers that depended on other primitive reducers and do so with async fetch calls? Did redux-promise-middleware/redux-saga seem end up feeling like a good fit?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@gaearon great, I get your points.
Still, one more thing.
If instead of solving this invite request thing using
redux-promise-middleware
(REQUEST_INVITE_PENDING, REQUEST_INVITE_FULFILLED, REQUEST_INVITE_REJECTED), doing so using sagas and having one and only saga that takes care of the inviting stuff.It would watch
REQUEST_INVITE
and when resolved it would do something like thisyield put( appActions.preloadData( {personData: {firstName: "Jorge"}}) )
This way, the invitation thing is only managed and understood in one place. I still get the part that it might become a mess later on in big apps because you can not check the "app" reducer and instantly get "who changed this state".
So the question final question is, is a matter of preference or I am still missing a better approach?
Thank you so much. 🎄