Created
February 3, 2019 14:25
-
-
Save carlrip/6fb4a1794b59afc3aeb33961d33f34a4 to your computer and use it in GitHub Desktop.
React Redux Reducer with TypeScript
This file contains hidden or 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
| 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