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
How to use useMemo instead of a local state using javascript and react?

i want to use useMemo instead of a local state using javascript, react and graphql.

what i am trying to do?

I am displaying a progress bar based on data fetched from progress query. the fetched data from progress query is set in a state.

below is the code,

const ProgressModal = (status) => {
    const [progress, setProgress] = React.useState<>(undefined); //progress state 
    //setting
    
    const { data: progressData, stopPolling: stopPolling } = 
        useCheckProgressQuery({
            variables: {id},
            pollInterval: 3000,
        })
   
    React.useEffect(() => {
        if (status === initial) {
            setProgress(undefined);
        }
        if (status===started) {
            setProgress(progressData);
        }
        if (status === finished && completed >= total || status === failed) {
            stopPolling();
            setProgress(undefined);
        }
    }, [progress, progressData, setProgress]);

    const completed= progress
        ? progress.Progress.completed : 0;
    const total = progress ? progress.Progress.total : 0;
    let value = 0; 
    if (completed > 0 && total > 0) {
        value = (completed / total) * 100;
    }

    return (
        <ProgressBar value = {progress} />
    );
}

the above code works but how can i use useMemo for above case instead of a local state. could someone help me with this. i am new to using react hooks. thanks.

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

0

useMemo and useState with useEffect do different things, so you can not convert useState/useEffect 100 % equally to useMemo.

A more or less equivalent useMemo approach would be this (but it doesn't work, other refactoring would then also be necessary, see below):

const progress = useMemo(() =>{
  if( status === initial ){
    return undefined;
  }
  if( status===started ){
    return progressData;
  }
  if( status === finished && completed >= total || status === failed ){
     return undefined);
  }
  return undefined; // <-- you need to define default/fallback
},
[ progressData, status, completed, total ] // <-- some where missing in your example
); 

This is not a working solution, more refactoring is required:

  • Here e.g. stopPolling() is not called, which needs an extra useEffect now.
  • progress depends on completed and total, and completed / total both depend on progress (circular dependencies)
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