Search code examples
javascriptreactjsecmascript-6reduxreact-redux

Is there way to destructure in reducer new added object?


I'm learning redux on app, that when you click day in calendar, it show form. After submit redux should take data from this form and and put in state like this date:[obj, obj, obj]. -> // date == activeDate

I can't destructure date, so it mutates and I have only one object in date array each time I hit submit.

    case SET_DATE:
       return {
         ...state,
          activeDate: action.payload.date
       }
    case ADD_CUT:
    const {date, name, hour, minute, kind} = action.payload
        return {
            ...state,
            appointments: {
                ...state.appointments,
                [date]: [
                       // here i need to destructure sth like
                       // ...state.appointments.date
                       // or state.appointments.activeDate
                    {
                        name: name,
                        hour: hour,
                        minute: minute,
                        kind: kind
                    }
                ]
            }
    }

How I can destructure it? Or maybe I should take data in different way?


Solution

  • I would keep the appointments in an array, and simply add new appointment to it like this:

    
    case ADD_CUT:
        const {date, name, hour, minute, kind} = action.payload;
       return {
           ...state,
           appointments: [
               ...state.appointments,
                   {
                       date,
                       name,
                       hour,
                       minute,
                       kind
                   }
              ]
      }