reactjs - Redux reducer todolist Immutable equivalent -
i'm trying rewrite redux todo example in immutable.js, far i've made initalstate immutable list , changed add , unshift rather spread operator, seems working fine. form immutable docs seem can use these unshift , push etc.. on list, think might ok?.
i've seen examples instead of .list() used .fromjs() wrapping object that's being push / unshift etc in .fromjs()
so, question
is that's required make immutable ?
const initialstate = immutable.list([{ text: 'use redux', marked: false, id: 0 }]); export default function todos(state = initialstate, action) { switch (action.type) { case add_todo: return state.unshift({ id: state.reduce((maxid, todo) => math.max(todo.id, maxid), -1) + 1, completed: false, text: action.text }); case delete_todo: return state.filter(todo => todo.id !== action.id ) case edit_todo: return state.map(todo => todo.id === action.id ? object.assign({}, todo, { text: action.text }) : todo ) case complete_todo: return state.map(todo => todo.id === action.id ? object.assign({}, todo, { completed: !todo.completed }) : todo ) case complete_all: const areallmarked = state.every(todo => todo.completed) return state.map(todo => object.assign({}, todo, { completed: !areallmarked })) case clear_completed: return state.filter(todo => todo.completed === false) default: return state } }
Comments
Post a Comment