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

149
Visualizações
How to trigger redux action in react componentDidUpdate method?

I have following action in redux action.js file,

export const removeActiveLayer = (id) => (dispatch) => {
  dispatch({
    type: REMOVE_ACTIVE_LAYER,
    payload: id,
  });
};

my reducer.js file is as below,

    case REMOVE_ACTIVE_LAYER:
      return {
        ...state,
        activeLayers: state.activeLayers.filter((l) => action.payload !== l.id),
      };

I tried to remove the layer from the RemoveLayer.js file as below,

this.props.removeLayer(55);

The layer is removed successfully. There is no issue. My question is, How can I get that id=55 from another file Layers.js?

I have tried to get it in ComponentDidUpdateProps as below,

componentDidUpdate(prevProps, prevState) {
    const { removeActiveLayer } = this.props;

    if (removeActiveLayer) {
      console.log(removeActiveLayer);
    }
}

It is just a function, but actually, I need a removed id. Any help will be highly appreciated.

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

0

You need to save the removed id in redux state as well if you want to access it in the some other component.

So your reducer.js file would become:

case REMOVE_ACTIVE_LAYER:
      return {
        ...state,
        removedId: action.payload,
        activeLayers: state.activeLayers.filter((l) => action.payload !== l.id),
      };

And now you can access the removedId as a prop from any other component you want:

componentDidUpdate(prevProps, prevState) {
    const { removedId } = this.props;

    if (removedId) {
      console.log(removedId);
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Since at state you already have activeLayers, just simply add a new sub state called lastRemovedLayer. Your state will look like this:

state = {
    // others,
    activeLayers: [],
    lastRemovedLayer: null, // it will be your layer id
}

then at reducer

case REMOVE_ACTIVE_LAYER:
    return {
        ...state,
        activeLayers: state.activeLayers.filter((l) => action.payload !== l.id),
        lastRemovedLayer: l.id
    };

Now you can access this last removed layer in any component at any moment/lifecycle you wish

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