Normalize data redux and using

82 Views Asked by At

I have the following data coming from an API:

const feed = {
  id: "feedId",
  content: "...",
  comments: [
    {
      id: "commentId",
      content: "...",
      // reply comments
      comments: [{
        id: "commentId",
        content: "..."
      }]
    }
  ],
}

I want to normalize the data for use in Redux as presented here. But my data has reply comments nested. I dont know where to place comment reply. Should I use normalize data and normalizr lib ?. Thank a lot If you answer and explain for me. Sorry about my bad english, but I really want know.

// where place comments reply ???
{
  feeds: {
    byId: {
      "feed1": {
        id: "feed1",
        content: "...",
        comments: ["comment1", "comment2"],
      },
      "feed2": {
        id: "feed2",
        content: "...",
        comments: ["comment3", "comment4"],
      },
    },
    allIds: ["feed1", "feed2"],
  },
  comments: {
    byId: {
      "comment1": {
        id: "comment1",
        content: "...",
      },
      "comment2": {
        id: "comment2",
        content: "...",
      },
      "comment3": {
        id: "comment3",
        content: "...",
      },
      "comment4": {
        id: "comment4",
        content: "...",
      },
    },
    allIds: ["comment1", "comment2", "comment3", "comment4"],
  },
};

This is my current init feedReducer for handle feed and comments in reducer.

const initialState = {
  feed_id_1: {
    id: "feed_id_1",
    content: "...",
    comments: [
      {
        id: "comment_id_1",
        content: "...",
        comments: [
          { id: "comment_reply_id_1", content: "..." },
          { id: "comment_reply_id_2", content: "..." },
        ],
      },
      {},
    ],
  },
  // ...
};
0

There are 0 best solutions below