Reactjs 在初始渲染时未接收到预期的重复制状态

Reactjs 在初始渲染时未接收到预期的重复制状态,reactjs,redux,Reactjs,Redux,我有一个社交媒体应用程序,其中每个帖子的commentCount值最初都没有呈现 Redux开发工具告诉我,我们的post对象没有接收到commentCount 只有在触发类似或不同操作后,post对象才会收到commentCount 这是我的数据缩减器 const initialState = { posts: [], post: {}, loading: false }; export default function(state = initialState, a

我有一个社交媒体应用程序,其中每个帖子的
commentCount
值最初都没有呈现


Redux开发工具告诉我,我们的post对象没有接收到
commentCount


只有在触发
类似
不同
操作后,
post
对象才会收到
commentCount

这是我的数据缩减器

const initialState = {
  posts: [],
  post: {},
  loading: false
};

export default function(state = initialState, action) {
  switch (action.type) {
    case LOADING_DATA:
      return {
        ...state,
        loading: true
      };
    case SET_POSTS:
      return {
        ...state,
        posts: action.payload,
        loading: false
      };
    case LIKE_POST:
    case UNLIKE_POST:
            let index = state.posts.findIndex(
        (post) => post.postId === action.payload.postId
            );
            state.posts[index] = action.payload;
            if (state.post.postId === action.payload.postId) {
                state.post = {...state.post, ...action.payload}
            }
      return {
        ...state
      };
    case SET_POST:
      return {
        ...state,
        post: action.payload
      };

    ...
  }
}
这些是我的行动

export const getPosts = () => (dispatch) => {
  dispatch({ type: LOADING_DATA });
  axios
    .get('/posts')
    .then(res => {
      dispatch({
        type: SET_POSTS,
        payload: res.data
      });
    })
    .catch(err => {
      dispatch({
        type: SET_POSTS,
        payload: []
      });
    });
};

export const likePost = (postId) => (dispatch) => {
  axios
    .get(`/post/${postId}/like`)
    .then(res => {
      dispatch({
        type: LIKE_POST,
        payload: res.data
      });
    })
    .catch(err => console.log(err));
};

export const unlikePost = (postId) => (dispatch) => {
  axios
    .get(`/post/${postId}/unlike`)
    .then(res => {
      dispatch({
        type: UNLIKE_POST,
        payload: res.data
      });
    })
    .catch(err => console.log(err));
};
网络选项卡


有什么建议吗?谢谢。

您是说服务器端的数据不包括评论数?“网络”选项卡中有什么?很可能服务器没有返回commentCount属性。你能检查一下JSON的回复吗?Hello@DaveNewton,我刚刚用网络标签的截图更新了帖子。是的,第一个Redux开发工具屏幕截图显示了我们的第一个post对象,没有commentCountHey@JoseFelix。Right
commentCount
最初未返回。您可以在我上面发布的Redux Dev Tools屏幕截图上找到JSON响应。@KennethAP然后您或团队成员必须更新后端,以便它可以向您发送
commentCount
您是说服务器端的数据不包括注释计数?“网络”选项卡中有什么?很可能服务器没有返回commentCount属性。你能检查一下JSON的回复吗?Hello@DaveNewton,我刚刚用网络标签的截图更新了帖子。是的,第一个Redux开发工具屏幕截图显示了我们的第一个post对象,没有commentCountHey@JoseFelix。Right
commentCount
最初未返回。您可以在我上面发布的Redux Dev Tools屏幕截图上找到JSON响应。@KennethAP然后您或团队成员必须更新后端,以便它可以向您发送
commentCount