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

162
Visualizações
How to make a reusable function in react?

I have a function that does the same thing under 2 difference circumstances inside of if statement but the function has been called in a different module ? what is the best way to handle the issue I mean I could export the function and import it to the other module but I don't like the idea that I have to call the function ... is there any better design pattern ?

// Module
if(somthing is true){
const functionSave =()=>{
// function logic 
}
functionSave()
}

      const saveFile = () => {
        if (_miscContext.modal.group === 'a') {
          // change file privlige
          localStorage.setItem('user', JSON.stringify('a'));
        }
        const ls = JSON.stringify(localStorage);
        
        const downloadBlob = (fileName: string, blob: any) => {
          const link = document.createElement('a');
          link.href = URL.createObjectURL(blob);
          link.target = '_blank';
          link.download = fileName;
          document.body.appendChild(link);
          link.click();
        };
        const blob = new Blob([ls], {
          type: 'text',
        });

        if (_miscContext.modal.group === 'a' || !_miscContext.modal.group) {
          downloadBlob('test', blob);
        } else if (_miscContext.modal.group === 'b') {
          downloadBlob(`test2`, blob);
        }
        // Tracking dowload
        ReactGA.event({
          category: 'Click',
          action: 'Saving File',
        });
        _miscContext.SetModal(false, '', 'preference');
        if (_miscContext.modal.group === 'a') {
          localStorage.setItem('user', JSON.stringify('defualt'));
        }
        return;
      };

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