Skip to content

Instantly share code, notes, and snippets.

@zhirzh
Last active March 3, 2018 14:24
Show Gist options
  • Save zhirzh/26e0556e8710e6337aa14ac4ba06613a to your computer and use it in GitHub Desktop.
Save zhirzh/26e0556e8710e6337aa14ac4ba06613a to your computer and use it in GitHub Desktop.
+type ClearAction = {type: 'CLEAR'};
+type ResetAction = {type: 'RESET', text: 'Hello World'};
+type UpdateAction = {type: 'UPDATE', text: string};
+
+type Action =
+ | ClearAction
+ | ResetAction
+ | UpdateAction;
+
const CLEAR = 'CLEAR';
const RESET = 'RESET';
const UPDATE = 'UPDATE';
-function reducer(state: string = '', action) {
+function reducer(state: string = '', action: Action) {
switch (action.type) {
case CLEAR:
return '';
type ClearAction = {type: 'CLEAR'};
type ResetAction = {type: 'RESET', text: 'Hello World'};
type UpdateAction = {type: 'UPDATE', text: string};
type Action =
| ClearAction
| ResetAction
| UpdateAction;
const CLEAR = 'CLEAR';
const RESET = 'RESET';
const UPDATE = 'UPDATE';
function reducer(state: string = '', action: Action) {
switch (action.type) {
case CLEAR:
return '';
case RESET:
return action.text;
case UPDATE:
return action.text;
default:
return state;
}
}
function clear() {
return { type: CLEAR };
}
function reset() {
return { type: RESET, text: 'Hello World' };
}
function update(text: string) {
return { type: UPDATE, text };
}
const CLEAR = 'CLEAR';
const RESET = 'RESET';
const UPDATE = 'UPDATE';
function reducer(state: string = '', action) {
switch (action.type) {
case CLEAR:
return '';
case RESET:
return action.text;
case UPDATE:
return action.text;
default:
return state;
}
}
function clear() {
return { type: CLEAR };
}
function reset() {
return { type: RESET, text: 'Hello World' };
}
function update(text: string) {
return { type: UPDATE, text };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment