h_a86 h_a86 -4 years ago 99
React JSX Question

Using spread operator from es6 in react-redux app with stage-2 loader

I am using ES6 spread operator in my react-redux app like this way

var defaultState = {val:1, items:[]};
export default function(state=defaultState, action){
switch(action.type){
case 'ADD_BOOK':
state.val = 0;
return {
...state,
items:action.payload.data.result
};


The problem is each time I get fresh data from
action.payload.data.result
assigned to items. I want to have concatenated data from previous state. Can someone help.

Answer Source

This should work.

return {
  ...state,
  items: [
    ...state.items,
    ...aciton.payload.data.result
  ]
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download