I have two cases in my reducer:
case GET_CODE:
return {
...state,
list:[...state.list,{code: action.payload.product.Code}]
}
case WEIGHT:
return {
...state,
list:[...state.list,{weight:action.payload}]
}
My state is looking like this:
list: [
{ weight: ''},
{weight: '100'},
{weight: '200'},
{code: '63'},
{code: '64'}
]
What i would like to have is something like:
list: [
{weight: ''},
{weight: '100', code:'63'},
{weight: '200', code:'64'}
]
My initial state is list:[] . First empty value of weight is from initial render i think.I dispatch first WEIGHT then GET_CODE action.
You should write some sort of helper function that creates the object for you, or the code/weight case should find the object in list it should correspond to.
For the helper function:
const createObject = (code, weight) => {
const obj = {code: code.product.Code, weight: weight}
dispatch({type: 'ADD_TO_LIST', payload: obj});
}
// In your reducer
case ADD_TO_LIST:
return {
...state,
list: [
...state.list,
action.payload
]
}
Or just use ADD_TO_LIST case and create the object in the reducer
i.e.
case ADD_TO_LIST:
return {
...state,
list: [
...state.list,
{code: action.payload.code.product.Code, weight: action.payload.weight}
]
}