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

131
Visualizações
I am not properly mutate the state of my redux store

I am creating a search functionality in my application with the help of redux. But it is not working as expected.

My initial state looks like this:

const initialState = {
  //some other state data
    locationSummaries: [],
}

My reducer function to update this locationSummaries array is given as:

const updateLocationSummaries = (state, action) => {
      state.locationSummaries = [...action.payload];
    },

My function to filter the array based on input search is given as:

 const handleInputSearch = (e) => {
    const { value } = e.target;
    const searchText = value.toLowerCase().trim();
    let filteredLocations = locationSummaries.filter((locationSummary) =>
      locationSummary.locationName.toLowerCase().startsWith(searchText)
    );
    dispatch(updateLocationSummaries(filteredLocations));
  };

Here searchText is the value that I am entering in the input text box

So in this, I am filtering the array lists when I am typing the text in the input search box.

After dispatching the filtered lists, my state is getting updated. But my locationSummaries array is eventually getting emptied. So what am I doing wrong in this scenario?

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

0

The issue is you update the same array.

  1. Keep another array to store filtered values.
const initialState = {
  //some other state data
  locationSummaries: [],
  filteredLocationSummaries: [],
}
  1. Update that locationSummaries array in the ation.
const updateLocationSummaries = (state, action) => {
      state.filteredLocationSummaries = [...action.payload];
},
  1. In the component use filteredLocationSummaries to render the filtered values.
about 4 years ago · Juan Pablo Isaza Relatório

0

The better answer here is don't keep the filtered values in state.

Instead, keep the original data in Redux, keep a description of how to filter the data in the component, and do the filtering while rendering:

const items = useSelector(state => state.some.items);
const [filterText, setFilterText] = useState('');

const filteredItems = useMemo(() => {
  if (!filterText) return items;
  return items.filter(item => item.includes(filterText);
}, [items, filterText]);
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