Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to export multiple functions Javacript?

I want to export default all functions inside export default, but every time I try to import them in another file, I get the error that the functions don’t exists. Am I importing wrong?

export default () => {

  // methods
  const makeRequest = async () => {

    async function useAllBenefits() {
      ...
    return payload;
    }

    async function useTopBenefits() {
     ...
    return payload;

    }

    // exposed
    return {
      useAllBenefits,
      useTopBenefits,
      makeRequest
    };
  };
}

myotherfile.js

import all from '../myFile'

console.log(all.useAllBenefits)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want to export multiple functions AS DEFAULT you can do it by exporting one object whose properties are functions.

export default {
   function1: () => {},
   function2: () => {},
   function3: () => {}
}

Now in another file you can import them and use as bellow:

import all from '../myFile.js';
all.function1();
all.function2();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading