Last active
August 1, 2016 17:20
-
-
Save andretw/3d4d0f4edc7f48990c5a1396908c766d to your computer and use it in GitHub Desktop.
reactjs-redux-change-state-in-action-creator-or-reducer
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 * as ACTION_TYPES from '../constants/action-types' | |
const action = (type, payload = {}) => ({ type, payload }) | |
const getUsernameSuccess = username => | |
action( | |
ACTION_TYPES.GET_USERNAME_SUCCESS, { | |
username, | |
} | |
) | |
export { | |
getUsernameSuccess, | |
} |
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 * as ACTIONS from '../constants/action-types' | |
export default function (state = {}, action) { | |
const { type, payload } = action | |
switch (type) { | |
case ACTIONS.GET_USERNAME_SUCCESS: | |
return { | |
...state, | |
username: payload.username, | |
message: 'Anything you want to show here.', | |
} | |
... | |
default: | |
return state | |
} | |
} |
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
function * watchGetUsernameRequest () { | |
while (true) { | |
try { | |
const { username } = yield take(ACTION_TYPES.GET_USERNAME) | |
yield put(ACTIONS.getUsernameSuccess(username)) | |
} catch (err) { | |
console.error('error occurred', err) | |
} | |
} | |
} | |
export default function * watchUserRegisterFlow () { | |
yield [ | |
fork(watchGetUsernameRequest), | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment