My Redux reducers receive posts from api and I store them in the store as object to store posts by their id's.
reducer.js
const initialState = {
posts: {},
postsByCategory: {},
addingPost: false,
}
export function posts(state = initialState, action){
enableES5()
return(
produce(state, draft => {
switch (action.type) {
case GET_POSTS:
action.payload.map(post => {
draft.posts[post.id] = post
})
break
default:
return draft
}
}))
}
The problem is I get the posts as sorted from highest id to lowest but while assigning to state, they become sorted from lowest id to highest which causes oldest post to seen at first and latest post at last.
I know if I store them in an array the problem will be solved but I need by their id's to make mutations easier. So how can I overcome this issue?
P.S: I tried something like this, but it didn't work =>
export function postsByIndex(state=[], action){
enableES5()
return(
produce(state, draft => {
switch(action.type){
case GET_POSTS:
draft = posts(state.posts, action)
default:
return draft
}
})
)
}
