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

99
Visualizações
Accessing Redux selector value in a custom hook

I'm writing my first hook and I also use redux. I'm listening to orientation sensor data from the mobile device in React native and when it updates I filter it to see if the new value is different than the old value before I send it over to redux state to be stored.

I'm noticing in the filterData method the data is always null. But if I console log the storedVal in the render I see it's always being updated and it's updated in the Redux store as well.

But for some reason filterData only has access to the initial value and doesn't get the updated values. Any idea how to fix this?

  const storedVal = useSelector(selectors.selectStoredVal);

  const filterData = useCallback(
    () => {
      console.log(storedVal);
    },
    [storedVal]
  );

  useEffect(
     () => {
        orientation.pipe(filter(filterData)).subscribe((data) => {
             console.log('Data filtered correctly, new data received here');
        }
     }
  ), 
  [];
}

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

0

An empty dependency array for useEffect means that the callback function within it will only run once (on component mount).

useCallback is creating a function that is being passed into the filter function. Because storedVal is a dependency for filteredData, went storedVal changes and filteredData is updated to a new function, your useEffect still has a reference to it's old value.

Try adding [filteredData] to your dependency array for the useEffect.

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