Introduction: modules and sharing code

How to export a function or variable

// myFunction.js
export default myFunction () { 
	// Your code here
 }

How to Import a function or variable

// consumerComponent.js
import myFunction from 'recipe/myFunction';

Export named functions or variables

// mortage.js
const getTermOptions = () => {
    return [
        { label: '20 years', value: 20 },
        { label: '25 years', value: 25 },
    ];
};

const calculateMonthlyPayment = (principal, years, rate) => {
    // Logic
};

export { getTermOptions, calculateMonthlyPayment };
import { getTermOptions, calculateMonthlyPayment } from 'recipe/mortgage';
export * from './utils';

Calling external APIs using Javascript

By default, you can’t make WebSocket connections or calls to third-party APIs from JavaScript code. To do so, add a remote site as a CSP Trusted Site.