Skip to content

Instantly share code, notes, and snippets.

@alaboudi
Last active March 18, 2019 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alaboudi/fcf258b4c78706543bfee38fbd18e896 to your computer and use it in GitHub Desktop.
Save alaboudi/fcf258b4c78706543bfee38fbd18e896 to your computer and use it in GitHub Desktop.
Declarative Action Type
// scoreboard-reducer.js
const INITIAL_STATE = {
home: 0,
away: 0,
}
export const scoreboardReducer = (state = INITIAL_STATE, action) {
switch (action.type) {
case 'GOAL_SCORED':
const scoringSide = action.payload.team;
return {...state, [scoringSide]: state.[scoringSide] + 1 };
default:
return state;
}
}
// audience-reducer.ts
const INITIAL_STATE = 0; // represents audience excitement;
export const audienceExcitementReducer = (state = INITIAL_STATE, action) {
switch (action.type) {
case 'GOAL_SCORED':
return state + 1; // audience gets more excited
default:
return state;
}
}
// Some container component.js
export class Game extends React.Component {
....
onGoal(scoringTeam) {
this.props.dispatch({type: 'GOAL_SCORED', payload: {team: scoringTeam}});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment