Search code examples
javascriptreactjsreduxstore

Reducer - add new element


I have this store:

const initialActors = {
    list: 'Actor\'s lists',
    actors: [
        {
            name: 'Angelina Jole',
            involved: true
        },
        {
            name: 'Bratt Pitt',
            involved: false
        },
    ]
}

I have a reducer to add a new actor to my store:

const actors = (state = initialActors, action) => {
    switch(action.type){
        case 'ADD_NEW':
            return {
                ...state.list,
                actors: [
                    ...state.actors,
                    action.name,
                    action.involved
                ]
            }
        default:
            return state
    }
}

I have a action creator too but it doesn't important. I dispatch that and display my state:

store.dispatch(addNew({name: "Hello", involved: true}) ); // I have this action creator
console.log(store.getState()

console.log displays very difficult object with letters like: {0: "A", 1: "c"}. What is wrong?

@Edit I tryed change ...state.list to state list like this:

const actors = (state = initialActors, action) => {
    switch(action.type){
        case 'ADD_NEW':
            return {
                state.list,
                actors: [
                    ...state.actors,
                    action.name,
                    action.involved
                ]
            }


        default:
            return state
    }
}

but I have this error:

Parsing error: Unexpected token, expected "," Similiar situation I have if I tryed modify actors array:

case 'ADD_NEW':
    return {
        ...state.list,
        actors: [
            ...state.actors,
            {
                action.name,
                action.involved
            }

        ]
    }

Error message is this same but point at action object (action.name).


Solution

  • state.list is a string, and you are trying to spread it

    let a = 'name'
    let c = {...a}
    console.log(c)

    run the above code snippet so you will be able to understand it

    and for adding new object you need to update the reducer as follows

                    ...state,
                    actors: [
                        ...state.actors,
                        {  
                           name: action.name,
                           involved: action.involved
                        }
                    ]
    

    so the above code will spread all the existing properties in the state and then you spread all the current actors and add a new object as shown above

    Update

    Since you are passing as

    store.dispatch(addNew({name: "Hello", involved: true}) );
    

    here the payload is an object so you can directly use that one

    so in action it will be object which has two props one is

    • type
    • the payload you have send it

                      ...state,
                      actors: [
                          ...state.actors,
                          action.payload
                      ]
      

    Sample Working codesandbox