Skip to content

Instantly share code, notes, and snippets.

@oaluna
Created January 18, 2020 07:53
Show Gist options
  • Save oaluna/14ec344092bdc4534d66f32b7cd0769a to your computer and use it in GitHub Desktop.
Save oaluna/14ec344092bdc4534d66f32b7cd0769a to your computer and use it in GitHub Desktop.
Reducers resource for React Redux.
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