To share code between components, use ES6 module and export the variables or functions that you want to share.
Your module structure looks like this:
mynamespace
└──myLibrary
└──myLibrary.js
// myFunction.js
export default myFunction () {
// Your code here
}
// consumerComponent.js
import myFunction from 'recipe/myFunction';
// mortage.js
const getTermOptions = () => {
return [
{ label: '20 years', value: 20 },
{ label: '25 years', value: 25 },
];
};
const calculateMonthlyPayment = (principal, years, rate) => {
// Logic
};
export { getTermOptions, calculateMonthlyPayment };
mortage.js
module is in the recipe
namespace.import { getTermOptions, calculateMonthlyPayment } from 'recipe/mortgage';
export * from './utils';
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.