Created
January 18, 2020 07:53
-
-
Save oaluna/14ec344092bdc4534d66f32b7cd0769a to your computer and use it in GitHub Desktop.
Reducers resource for React Redux.
This file contains 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 { combineReducers } from 'redux' | |
import { | |
ADD_TODO, | |
TOGGLE_TODO, | |
SET_VISIBILITY_FILTER, | |
VisibilityFilters | |
} from './actions' | |
const { SHOW_ALL } = VisibilityFilters | |
function visibilityFilter(state = SHOW_ALL, action) { | |
switch (action.type) { | |
case SET_VISIBILITY_FILTER: | |
return action.filter | |
default: | |
return state | |
} | |
} | |
function todos(state = [], action) { | |
switch (action.type) { | |
case ADD_TODO: | |
return [ | |
...state, | |
{ | |
text: action.text, | |
completed: false | |
} | |
] | |
case TOGGLE_TODO: | |
return state.map((todo, index) => { | |
if (index === action.index) { | |
return Object.assign({}, todo, { | |
completed: !todo.completed | |
}) | |
} | |
return todo | |
}) | |
default: | |
return state | |
} | |
} | |
const todoApp = combineReducers({ | |
visibilityFilter, | |
todos | |
}) | |
export default todoApp | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment