1

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?

1
  • What is the value of your state.buildings array before slicing ? Commented Sep 27, 2016 at 14:22

2 Answers 2

1

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 }
    );
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.