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
How to access prevProps' in React Hooks when prevProps' data type is object or array

My question is this, I'm having an array I need to monitor its change in my componentDidUpdate lifecycle, now I'm trying to rewrite the component in hooks, I'm having issue in doing so in hooks, a lot of the solutions I found on the internet suggests to create a customized usePrevious hook like this

    const usePrevious = value => {
      const ref = useRef();
      useEffect(() => {
        ref.current = value;
      });
      return ref.current;
    };

But I found that this hook can only deal with primitive data type, it doesn't seem to work with object or array...

  componentDidUpdate(prevProps) {
    if (!_.isEqual(myArray, prevProps.myArray)) {
      // do something...
    }
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

From what you're saying I think using a useEffect hook that has the prop you want to watch for as dependency and the logic inside should do the trick, you can check the React Docs for a more detailed explanation, but basically, it would look like this

useEffect(() => {
  // code to run when it changes
}, [yourVar]);
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