'Normalizing state in Redux

I tried to google some real-world examples of using normalized state in redux, but nothing useful was found, so, I'm here to try clarify some moments about splitting normalized state during app lifecycle.

In my app I have
~ users and posts entities
~ search page where I can search users by name and check their posts

For search page I have searchReducer.
In usual way, for searching users, I'll just send request (/users?search='eugene') and then save found users in my searchReducer:

// searchReducer

const foundUsers = [
  {
    id: 1,
    name: 'Eugene',
    posts: [
      {
        id: 24,
        title: 'Hello',
        text: 'Lorem ipsum',
      },
      {
        id: 26,
        title: 'Goodbye',
        text: 'Ipsum lorem',
      }
    ],
  },
  {
    id: 2,
    name: 'Eugene',
    posts: [],
  }
]

How can I change this to normalized state?
If I understand everything right, I have to split this data into 2 global reducers, 'users' and 'posts?

// usersReducer
const users = {
  byId: {
    1: {
      id : 1,
      name: 'Eugene',
      posts: [24, 26],
    },
    2: {
      id : 2,
      name: 'Eugene',
      posts: [],
    },
  },
  allIds : [1, 2],
}

// postsReducer
const posts = {
  byId: {
    24: {
      id: 24,
      title: 'Hello',
      text: 'Lorem ipsum',
      userId: 1,
    },
    26: {
      id: 26,
      title: 'Goodbye',
      text: 'Ipsum lorem',
      userId: 1,
    },
  },
  allIds: [24, 26],
}

And in searchReducer just create variable with usersIds:

const foundUsers = [1,2];

Is it correct way to organize normalized state?
It looks nice but how I understand, if I'll do a lot of searches (on /search page), I can get a huge amount of posts in postReducer that will lead to bad performance.
Of course, I can clear posts before doing new search, but in theory, some posts can be also be needed for other reducer in app, so here I can't find some good way of doing something like garbage collector.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source