I have many helper methods which I copy from one Salesforce Lightning component helper JavaScript file to another.

Example common helper method

callAction : function(cmp, methodName, params, callback){
    var action = cmp.get(methodName); 
    action.setCallback(this, function(response) {
        var state = response.getState();
        if(cmp.isValid() && state === "SUCCESS"){
            var result = response.getReturnValue();
            if (callback) callback(result);
        } else if (state === "ERROR"){
            var errors = response.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
    $A.getCallback(function() {

These generic style methods are useful in most components.

I would like a central location for all my common JavaScript methods which is accessible from all my custom Salesforce Lightning components.

And I would like this central location to be easily editable.

How could I achieve this with Salesforce Lightning?


Better approach is to have all reusable code inside static resource. Refer Sharing JavaScript Code Across Components.

We had performance issues with storing reusable JavaScript inside extendible components. Each component separately loads Its instance of extended class, with static resource they can be loaded just once when having a complex embedded components in a application.


I am not a fan of the inheritance for lightning components as currently implemented by Salesforce. So, I've made my own Service Component Design Pattern.

This is a pattern based off some previous blogs, but yes, I prefer to call methods (just a style thing) explicitly after dropping them into a component. This way I can create a bunch of micro-services and "compose" a bigger component from it.

FYI, you can take advantage of the fact all instances of the same component share one instance of a helper class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.