problems with slice() on react-redux

I am trying to copy a state with slice(), then manipulate it and create a new state

this is my Reducer:

case types.LOAD_AMOUNT_CHECKINS_SUCCESS: {
        let buildings = state.buildings.slice();

        buildings['devices_error'] = action.checkins['error];
        .
        .
        .
        return Object.assign({}, state,
            {buildings: buildings}
        );
    }

but applying slice() returns me: []

Where I have the error?

Upvotes: 1

Views: 583

Answers (2)

Yangshun Tay
Yangshun Tay

Reputation: 53169

Assuming state.buildings is an object (and not an array), change the second line to this:

case types.LOAD_AMOUNT_CHECKINS_SUCCESS: {
    let buildings = Object.assign({}, state.buildings);

    buildings['devices_error'] = action.checkins['error];
    .
    .
    .
    return Object.assign({}, state,
        { buildings: buildings }
    );
}

Upvotes: 1

Alex Parij
Alex Parij

Reputation: 1286

To make a shallow copy you can call slice() on arrays only https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

In you code example you are calling it on the object not on the array.

Upvotes: 1

Related Questions