Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda