Skip to content

Instantly share code, notes, and snippets.

@andretw
Last active August 1, 2016 17:20
Show Gist options
  • Save andretw/3d4d0f4edc7f48990c5a1396908c766d to your computer and use it in GitHub Desktop.
Save andretw/3d4d0f4edc7f48990c5a1396908c766d to your computer and use it in GitHub Desktop.
reactjs-redux-change-state-in-action-creator-or-reducer
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,
}
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
}
}
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