0

I'm trying to rewrite the redux todo example in immutable.js, so far I've just made the initalstate an immutable list and changed the add to and unshift rather than a spread operator, it seems to be working fine. form the immutable docs it seem to say you can use all these unshift , push etc.. on a list, so i think this might be ok?.

i've seen some examples where instead of .List() they used .fromJS() also wrapping the object that's being push / unshift etc in .fromJS()

so, my question

is this all that's required to make it 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
  }
}
user3224271
  • 1,214
  • 3
  • 13
  • 22

0 Answers0