I want to update the react state using redux but data is not sorting correctly
Original Array
"sections": [
{
"id": 8,
"user_id": 1,
"field_type_id": 8,
"section_id": 3,
"value": "+96******",
"type": "phone",
"url": "tel:",
"icon": "phone"
}
{
"id": 9,
"user_id": 1,
"field_type_id": 8,
"section_id": 3,
"value": "[email protected]",
"type": "email",
"url": "",
"icon": "email"
}
]
I am updating the state using this code.
state = { ...state,sections :[ ...state.sections.filter(
(section) => section.id !== action.payload.section.id
) , action.payload.section ] }
return state
After updating the array objects are getting reversed
"sections": [
{
"id": 9,
"user_id": 1,
"field_type_id": 8,
"section_id": 3,
"value": "[email protected]",
"type": "email",
"url": "",
"icon": "email"
}
{
"id": 8,
"user_id": 1,
"field_type_id": 8,
"section_id": 3,
"value": "+91344******",
"type": "phone",
"url": "tel:",
"icon": "phone"
}
]
How can i stop the array reversing?
splicefor that. It not related to react or redux. It's just plain vanilla JS....state,sections :[ ...state.sections.filter(? Are you using a spread as an object key here ? I'm surprised this is even valid syntax.