Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda