2016-11-26 25 views
0

所以,我有一个表的状态。如何更新状态数组内的对象?

const initialState = { 
allTables: [], 
fetching: true, 
tableId: {}, 
}; 

allTables是一个拥有许多表的对象的状态。

allTables: Array[16] 

并且每个对象都有id,current_order,表类型。

{ 
id: 1 
type: "main" 
current_order: null 
} 

所以,现在我所拥有的是当用户点击一个表时,它会创建顺序。我想添加的是,当用户单击一个表时,它将order_id发送到reducer,并更新table对象的current_order。

case Constants.TABLE_IS_BUSY: 
     return { ...state, tableId: action.id } 

    case Constants.TABLES_NEW_ORDER_CREATED: 
     return Object.assign({}, state, { 
      tables: state.allTables.map((id) => { 
       if (state.allTables.id === state.tableId) { 
        return Object.assign({}, { 
         state.allTables.current_order: action.order 
        }) 
       } 
       return state 
      }) 

     }) 

所以为了做到这一点,我做了如上所述的减速器。当你点击表格时,它会触发TABLE_IS_BUSY和TABLES_NEW_ORDER_CREATED。对于TABLE_IS_BUSY,我得到了用户点击的table的table_id,并更改了tableId的状态值。但对于TABLES_NEW_ORDER_CREATED,它不会更改current_user的值......我如何正确执行此操作?

在此先感谢..

回答

1

你不应该有这样的事情:

case Constants.TABLES_NEW_ORDER_CREATED: 
     return Object.assign({}, state, { 
      tables: state.allTables.map((table) => { 
       if (table.id === state.tableId) { 
        return Object.assign({}, table, { 
         current_order: action.order 
        }) 
       } 
       else { 
        return Object.assign({}, table) 
       } 
      }) 
     }) 
+0

感谢,我应该把表而不是ID的.. –

相关问题