Created
August 22, 2019 14:47
-
-
Save bpander/b7110aec30d7b067fdc671f11bfe0df7 to your computer and use it in GitHub Desktop.
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
| import { Reducer, Action } from 'redux'; | |
| interface CounterState { count: number } | |
| const initialState: CounterState = { count: 0 }; | |
| const ADD = 'ADD'; | |
| interface AddAction extends Action<typeof ADD> { | |
| payload: { amt: number }; | |
| } | |
| export const addAction = (payload: AddAction['payload']): AddAction => ({ | |
| type: ADD, payload, | |
| }); | |
| const SUBTRACT = 'SUBTRACT'; | |
| interface SubtractAction extends Action<typeof SUBTRACT> { | |
| payload: { howMany: number }; | |
| } | |
| export const subtractAction = (payload: SubtractAction['payload']): SubtractAction => ({ | |
| type: SUBTRACT, payload, | |
| }); | |
| type CounterAction = | |
| | AddAction | |
| | SubtractAction | |
| export const counterReducer: Reducer<CounterState, CounterAction> = (s = initialState, action) => { | |
| switch (action.type) { | |
| case ADD: return { ...s, count: s.count + action.payload.amt }; | |
| case SUBTRACT: return { ...s, count: s.count - action.payload.howMany }; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment