正在尝试使用数据库中的新数据更新redux状态



我有一个查询,它从数据库中获取前10个最近的帖子(id、postTitle、dateCreated等(,并调用reducer来更新groupsPosts状态我有另一个查询,它获取旧的10篇帖子,并调用reducer来更新组PostsMore状态,该状态存储检索到的旧的10条帖子groupPostsMoreButton更简单地存储true或false,如果它们是要加载的大多数帖子的话。所有这些都很好。

我正在输出带有地图功能的groupPosts

groupPosts.map((item, index) => (
< PostCard                        
key={index}
item={item}                       
/>
))
const groupPosts = useSelector(state => state.groups.groupPosts);
const morePostsButton = useSelector(state => state.groups.groupPostsMoreButton);
const groupPostsMore = useSelector(state => state.groups.groupPostsMore);

我的问题是,我无法将旧的10个帖子添加到当前的groupPosts状态。我已经尝试连接到groupPostsgroupsPosts: state.groupPosts.concat(action.groupPostsMore),,但它没有更新前端的状态。

这是我的减速器

import { GET_GROUP_POSTS, GET_GROUP_POSTS_MORE, GET_GROUP_POSTS_MORE_BUTTON } from "../constants";
const initialState = {

groupPosts: [],
groupPostsMore: [],
groupPostsMoreButton: null,
loaded: false
}
export const groups = (state = initialState, action) => {
switch (action.type) {        
case GET_GROUP_POSTS:
return { 
...state, 
groupPosts: action.groupPosts,
loaded: action.loaded
}
case GET_GROUP_POSTS_MORE_BUTTON:
return {
...state,
groupPostsMoreButton: action.groupPostsMoreButton,
loaded: action.loaded
}
case GET_GROUP_POSTS_MORE:
return { 
...state,  
groupPostsMore: action.groupPostsMore,                  
groupsPosts: state.groupPosts.concat(action.groupPostsMore),                                  
loaded: action.loaded
}
default:
return state
}
}

您可以尝试@reduxjs/toolkit。对于Redux代码,它有一个干净得多的语法。您的代码似乎因为这个拼写错误而无法工作:groupsPosts。应该是groupPosts,对吧?

嘿,如果以下内容没有帮助,

groupsPosts: [...state.groupPosts,...action.groupPostsMore]

然后你肯定需要检查action.groupPostsMore,那里记录了什么。

希望能有所帮助。无需怀疑

相关内容

  • 没有找到相关文章

最新更新