2017-05-11 23 views
0

我有從ImmutableJS Record對象和我想要的元件推到使用withMutations這樣內記錄屬性stack的數組:如何正確PUSH/POP的元素內記錄的陣列ImmutableJS

/* eslint-disable new-cap */ 
const InitialState = Record({ 
    isOpen: true, 
    item: null, 
    type: '', 
    stack: [] 
}); 

/* eslint-enable new-cap */ 
const initialState = new InitialState; 

export default function modal(state = initialState, action) { 
    ... 
    return state.withMutations((ctx) => { 
    const newStack = ctx.get('stack'); 
    newStack.push({ 
     item: action.item, 
     type: action.type 
    }); 

    ctx.set('isOpen', true) 
     .set('item', action.item) 
     .set('type', action.type) 
     .set('stack', newStack); 
    }); 
}); 

但是,這似乎在記錄中未設置stack屬性。 當我這樣做時,它正確地將數組設置爲stack

return state.withMutations((ctx) => { 
    ctx... 
    .set('stack', [{ 
     item: action.item, 
     type: action.type 
    }]); 
}); 

因此,

兩個問題

A.如何設置stack屬性與數組?

B.如何在ImmutableJS中將一個push/pop對象push到數組中?

回答

0

我落得這樣做是這樣的:

​​

流行

return state.withMutations((ctx) => { 
    ctx... 
     .update('stack', stack => stack.pop()); 
});