Skip to content

Instantly share code, notes, and snippets.

@carlrip
Created February 3, 2019 14:25
Show Gist options
  • Select an option

  • Save carlrip/6fb4a1794b59afc3aeb33961d33f34a4 to your computer and use it in GitHub Desktop.

Select an option

Save carlrip/6fb4a1794b59afc3aeb33961d33f34a4 to your computer and use it in GitHub Desktop.
React Redux Reducer with TypeScript
const peopleReducer: Reducer<IPeopleState, PeopleActions> = (
state = initialPeopleState,
action,
) => {
switch (action.type) {
case 'GettingPeople': {
return {
...state,
loading: true,
};
}
case 'GotPeople': {
return {
...state,
people: action.people,
loading: false,
};
}
case 'PostingPerson': {
return {
...state,
posting: true,
};
}
case 'PostedPerson': {
return {
...state,
posting: false,
people: state.people.concat(action.result.person),
};
}
default:
neverReached(action); // when a new action is created, this helps us remember to handle it in the reducer
}
return state;
};
// tslint:disable-next-line:no-empty
const neverReached = (never: never) => {};
const rootReducer = combineReducers<IAppState>({ peopleState: peopleReducer });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment