0

When running my script I get the error that my function is not defined. I think I am creating or calling my function inside of the Class incorrectly.

This one is supposed to be an IIFE (Immediately Invoked Function Expression) but I'm not sure if thats possible inside of a class. (The second part where StartCalender() is called is in another function in my script)

  startCalender = ((month) => {
    //contents
  })();


  startCalender(//variable);

The other function causing the same error I tried as an arrow function but changed it to see if it would fix the error:

  changeMonth(changeAmount) {

    //contents

    startCalender(//variable);
};

This is called in the return() of Render():

<BiLeftArrowAlt className="arrowIcon leftArrow" onClick={() => changeMonth(-1)}/>

This is just an Imported FontAwesome component which calls the previous function when clicked.

  • `setState` is a *method*, you must call it, not assign to a property of it. In order to refer to a property of an object, put a dot between the object and the property. `this.changeMonth` – CertainPerformance Apr 02 '21 at 22:11
  • That probably fixes something but the error i'm getting refers to calling the function specifically, not its contents: 'changeMonth' is not defined. I'm asking about calling the function correctly not about using state – Lucas Dowlen Apr 02 '21 at 22:13
  • *In order to refer to a property of an object, put a dot between the object and the property. `this.changeMonth`* – CertainPerformance Apr 02 '21 at 22:14
  • @CertianPerformance Thanks, It doesn't fix the error but it helps anyways. – Lucas Dowlen Apr 02 '21 at 22:18
  • It should. `this` will be defined when rendering, so accessing an existent property of the instance will work, assuming `changeMonth` is really defined as a method of the class (or as a class field) – CertainPerformance Apr 02 '21 at 22:19
  • @CertainPerformance Thats the issue, It says that 'changeMonth is not defined' – Lucas Dowlen Apr 02 '21 at 22:22
  • Sounds like you still aren't referring to it as a property of the instance, but as a standalone variable. No such standalone variable exists. – CertainPerformance Apr 02 '21 at 22:22
  • @CertainPerformance Even if I delete all of the functions contents, I still get the same error. Something is wrong with the Function or how I call it (probably syntax) but don't see the error – Lucas Dowlen Apr 02 '21 at 22:25
  • I've explained it multiple times by now, please read the comments again... you cannot refer to a property of an object as if it was a standalone variable, since it's not a standalone variable – CertainPerformance Apr 02 '21 at 22:31
  • @CertainPerformance Sorry, I really don't understand what you're saying. What do you suggest changing in the code to fix it? Like can you tell me exactly what I need to type to fix this? – Lucas Dowlen Apr 02 '21 at 22:36
  • if you are referring to startCalender = ((month)... The following function which is just a standard function doest run either – Lucas Dowlen Apr 02 '21 at 22:40
  • @CertainPerformance Sorry, I'm so stupid. That actually fixed it... I was changing the variables stored in state to this.stateValue. Anyways thanks for the help. – Lucas Dowlen Apr 02 '21 at 22:49

0 Answers0