2

Trying to figure out how to share JS functions with another component. I have followed the documentation on the LWC here

Here is the functions.js file reusableLWCFunctions.js

const myfunction = (myString) => {

    console.log(myString);
    return myString;
};

export{
    myfunction
};

Here is the LWC that calls the function.

Currently I get the following error: reuseableLWCFunctions.myFunction is not a function

import { myFunction } from 'c/reuseableLWCFunctions';

export default class myLWC extends NavigationMixin(LightningElement) {
    constructor(){
        super();
        myFunction('THIS WAS CALLED FROM IMPORTED FUNCTION');
    }

Not sure why it's breaking the component and not firing the console.log statement.

Any help would be great:

Already tried the solution on this post

5

you need to make this call from the connectedCallback() method. doing so will have resolved your import by this time. See below:

import { myfunction } from 'c/reusableLWCFunctions';

export default class myLWC extends NavigationMixin(LightningElement){

    connectedCallback(){
      myfunction('THIS WAS CALLED FROM IMPORTED FUNCTION');
    }

}

the constructor for your case would really be useless. in fact in most cases I don't even see the need in using it. connectedCallback is where you want to make any calls to your imports.

8
  • 1
    Trying now, will let you know result.
    – thinker
    Jan 10 '20 at 18:50
  • 1
    do you have the HTML template for this?
    – Ronnie
    Jan 10 '20 at 18:54
  • 1
    wait - let me fix my casing. i believe you may not be even importing it due to miscased method name. updating my response.
    – Ronnie
    Jan 10 '20 at 18:54
  • 1
    see if you fix your casing to match with your sample method that it takes care of this for you.
    – Ronnie
    Jan 10 '20 at 18:57
  • 2
    That did it. Thank you for your help.
    – thinker
    Jan 10 '20 at 19:04

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.