Last active
November 29, 2018 13:44
-
-
Save alaboudi/421b211c0597b34cac1eb3e6803a1714 to your computer and use it in GitHub Desktop.
Imperative Action Type
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
// scoreboard-reducer.js | |
const INITIAL_STATE = { | |
home: 0, | |
away: 0, | |
} | |
export const scoreboardReducer = (state = INITIAL_STATE, action) { | |
switch (action.type) { | |
case 'INCREMENT_HOME_SCORE': | |
return {...state, home: state.home + 1 }; | |
case 'INCREMENT_AWAY_SCORE': | |
return {...state, away: state.away + 1 }; | |
default: | |
return state; | |
} | |
} | |
// audience-reducer.js | |
const INITIAL_STATE = 0; // represents audience excitement; | |
export const audienceExcitementReducer = (state = INITIAL_STATE, action) { | |
switch (action.type) { | |
case 'INCREASE_AUDIENCE_EXCITEMENT': | |
return state + 1; // audience gets more excited | |
default: | |
return state; | |
} | |
} | |
// coupling in some container component.js | |
export class Game extends React.Component { | |
.... | |
onHomeGoal() { | |
this.props.dispatch({type: 'INCREMENT_HOME_SCORE'}); | |
this.props.dispatch({type: 'INCREASE_AUDIENCE_EXCITEMENT'}); | |
} | |
onAwayGoal() { | |
this.props.dispatch({type: 'INCREMENT_AWAY_SCORE'}); | |
this.props.dispatch({type: 'INCREASE_AUDIENCE_EXCITEMENT'}); | |
} | |
} |
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
// Another version of this implementation | |
// scoreboard-reducer.js | |
const INITIAL_STATE = { | |
home: 0, | |
away: 0, | |
} | |
export const scoreboardReducer = (state = INITIAL_STATE, action) { | |
switch (action.type) { | |
case 'INCREMENT_HOME_SCORE': | |
return {...state, home: state.home + 1 }; | |
case 'INCREMENT_AWAY_SCORE': | |
return {...state, away: state.away + 1 }; | |
default: | |
return state; | |
} | |
} | |
// audience-reducer.js | |
const INITIAL_STATE = 0; // represents audience excitement; | |
export const audienceExcitementReducer = (state = INITIAL_STATE, action) { | |
switch (action.type) { | |
case 'INCREASE_AUDIENCE_EXCITEMENT': | |
return state + 1; // audience gets more excited | |
default: | |
return state; | |
} | |
} | |
export class Game extends React.Component { | |
.... | |
onHomeGoal() { | |
this.props.dispatch({type: 'INCREMENT_HOME_SCORE'}); | |
} | |
onAwayGoal() { | |
this.props.dispatch({type: 'INCREMENT_AWAY_SCORE'}); | |
} | |
} | |
// coupling in side-effect-handler.js (using redux-saga) | |
export function* watchIncrementHomeScore() { | |
yield takeEvery(['INCREMENT_HOME_SCORE','INCREMENT_AWAY_SCORE'], updateAudienceExcitement); | |
} | |
export function* updateAudienceExcitement() { | |
yield put({ type: 'INCREASE_AUDIENCE_EXCITEMENT'}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment