React-redux Spread statement in an unexpected token reducer return error

I followed Dan Abramov's code at https://github.com/tayiorbeii/egghead.io_redux_course_notes/blob/master/08-Reducer_Composition_with_Arrays.md

I get the error "Unexpected token on line 22", referring to ... todo I did not think that this was due to Babel settings, because ... the state is working fine. When I replace ... todo with ... state inside the map function, it returns the same error.

///Reducer//
    export default (state=[], action) => {
      switch (action.type) {

        case 'ADD_TODO':
            return [...state,
                {
                 id:action.id,
                 text: action.text,
                 completed:false
                }
            ];

         case 'TOGGLE_TODO':
          return state.map(todo => {
            if (todo.id !== action.id) {
              return todo;
            }

            return {
              ...todo, //returning error
              completed: !todo.completed
            };
          });


        default:
            return state;
      }
     }

My call code:

it('handles TOGGLE_TODO', () => {
    const initialState = [
        {
        id:0,
         text: 'Learn Redux',
         completed: false
        },
        {
        id:1,
         text: 'Go Shopping',
         completed: false
        }
    ];


    const action = {
        type: 'TOGGLE_TODO',
        id: 1
    }




    const nextstate = reducer(initialState,action)



    expect (nextstate).to.eql([
        {
        id:0,
         text: 'Learn Redux',
         completed: false
        },
        {
        id:1,
         text: 'Go Shopping',
         completed: true
        }
    ])
+4
source share
1 answer

It's about presets.

Array propagation is part of the ES2015 standard, and you use it here.

        return [...state,
            {
             id:action.id,
             text: action.text,
             completed:false
            }
        ];

However here

        return {
          ...todo, //returning error
          completed: !todo.completed
        };

, , 2.

Babel: https://babeljs.io/docs/plugins/transform-object-rest-spread/ Object.assign (. )

+4

Source: https://habr.com/ru/post/1649605/


All Articles