Skip to content

Instantly share code, notes, and snippets.

@wuzzeb
Created September 29, 2019 18:06
Show Gist options
  • Save wuzzeb/b33e1866f165b880fc9a742ad7617881 to your computer and use it in GitHub Desktop.
Save wuzzeb/b33e1866f165b880fc9a742ad7617881 to your computer and use it in GitHub Desktop.
Sample typescript redux store
export interface SampleState {
num: number;
}
export enum SampleActionType {
IncrementNum = "Sample_Increment",
DecrementNum = "Sample_Decrement"
}
export type SampleAction =
| { type: SampleActionType.IncrementNum; by: number }
| { type: SampleActionType.DecrementNum };
export function sampleReducer(s: SampleState | undefined, a: SampleAction): SampleState {
if (s === undefined) {
return { num: 100 };
}
switch (a.type) {
case SampleActionType.IncrementNum:
return { num: s.num + a.by };
case SampleActionType.DecrementNum:
return { num: s.num - 1 };
default:
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment