Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!