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

128
Visualizações
(React) Redux state not filitering

I can change an item in my list if the item's boolean property value changes, my list updates. However, if I try to change a string property and return the payload my list isn't updated.

This works when I directly change the item and return it.

case UPDATED_LISTITEM:
  let newList = state.list.filter((item) => {
    if (item._id !== payload._id) {
      return item;
    } else {
      item.title = payload.title;
      item.checked = payload.checked;
      return item;
    }
  });

  return {
    ...state,
    list: newList,
  };

This doesn't work when I try to return the payload and I only update the 'title'. If I update the 'checked' the state returns properly.

case UPDATED_LISTITEM:
  return {
    ...state,
    list: state.list.filter((item) => {
      if (item._id !== payload._id) {
        return item;
      } else {
        return payload;
      }
    }),
  };

  
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There's a couple different problems here.

One is that this code is mutating the item, and that is never allowed when using Redux:

item.title = payload.title;
item.checked = payload.checked;

Redux requires immutable updates, which means always making copies of the data.

Another is that you're using the wrong array method. You're calling list.filter(), but you're always returning items. I think you want to use list.map() instead.

Beyond that, I'm afraid that the style of Redux code you're writing is very outdated. It looks like you're "hand-writing" your reducers, immutable updates, and action types.

Today, we teach "modern Redux" with Redux Toolkit and the React-Redux hooks API, which is much simpler to learn and use. The code you're writing will run, but it's going to require writing at least 4x as much code, and will be more confusing.

I'd strongly recommend reading through our official Redux docs tutorials, which teach Redux Toolkit and React-Redux hooks as the standard approach:

https://redux.js.org/tutorials/index

about 4 years ago · Juan Pablo Isaza Relatório

0

Version without mutating the state:

case UPDATED_LISTITEM:
  return {
    ...state,
    list: state.list.map((item) => ({
      ...item, // item must not contain more deeply nested objects!

      // Update title and checked from payload
      ...(
        item._id === payload._id ?
          { title: payload.title, checked: payload.checked } : {}
      )
    }),
  };

As the other answer stated, it's easier with redux toolkit. The old way involves carefully reconstructing the state, heavy use of spread operator (...). It's error prone with deeply nested objects.

about 4 years ago · Juan Pablo Isaza Relatório
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