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

191
Visualizações
State of my application is not working properly

I am creating delete functionality in my application. But somehow it is not behaving properly.

The initial state and reducer function is defined below:

import { createSlice } from '@reduxjs/toolkit';

const filterDataTemplate = {
  programId: '',
  year: '',
};

const initialState = {
   //some other state
  filterData: { ...filterDataTemplate },
};

const slice = createSlice({
  name: 'editFilterSlice',
  initialState: initialState,
  reducers: {    
    updateFilterProgramId: (state, action) => {
      return {
        ...state,
        filterData: {
          ...state.filterData,
          programId: action.payload,
        },
      };
    },
    updateFilterYear: (state, action) => {
      return {
        ...state,
        filterData: {
          ...state.filterData,
          year: action.payload,
        },
      };
    },    
  },
});

export const {
  updateFilterYear,
  updateFilterProgramId, 
} = slice.actions;

export default slice.reducer;

And filter details is the object containing year and programId:

filterDetails: {year:2021, programId: "Ameria"}

And i want to achieve

filterDetails: {year:"", programId: ""}

So delete handler is stated below to achieve above criteria:

const handleDelete = (e) => {    
    e.preventDefault();
    if (//some condition) {
      dispatch(updateFilterYear(''));
      console.log('filterdetails', filterDetails); 
      //filterDetails: {year:2021, programId: "Ameria"}
    } else {
      dispatch(updateFilterProgramId(''));  
    }
}.

//Outside handleDelete function
console.log('filterdetails', filterDetails); 
//filterDetails: {year:"", programId: "Ameria"}

So filter details containg year and programId is obtained with the help of this code:

const filterDetails = useAppSelector(
   (state) => state.locationsFilter.filterData
 );

handleDelete function is getting called properly when I am clicking a button

But after running this code, the filter details is not updating first time but when I am again running the handler second time it is updating. And outside handleDelete it is showing correct value in the console but inside handleDelete console is showing correct value second time.

It is showing very strange behavior

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