I want to receive a post by id. But I have a problem that if I open one message first and then open another message, then the other record will just be added to the state and it will look like this. Although only one entry should be displayed.
How to fix it?
reducer:
const postReducer = createReducer(
initialState,
on(loadPostSuccess, (state, action) => {
return postAdapter.setOne(action.post, state);
})
);
export function PostReducer(state: PostState | undefined, action: Action) {
return postReducer(state, action);
}
state:
export interface PostState extends EntityState<Post> {}
export const postAdapter = createEntityAdapter<Post>({
selectId: (post: Post) => post.post_id,
});
export const initialState: PostState = postAdapter.getInitialState({});
selector:
const selectPostState = createFeatureSelector<RequestState>(REQUEST_STATE_NAME);
export const selectRequest = createSelector(
selectPostState,
(state) => state.post
);
export const selectRequestEntities = createSelector(
selectRequest,
(state) => state.entities[state.ids[0]]
);