-2

The following code sits in a React Class Component (as class methods)

      _checkSelectedTime = () => {
    if (!this.state.selectedTime || !this.state.selectedTimeTwo) {
      this.setState({
        showSnackbar: true,
        snackbarMessage: formatMessage({
          id: 'app.monthlylogspage.noselectedtimeerror',
          defaultMessage: 'Error: Make sure that you have properly selected times.',
        }),
        selectedTimeCheck: false,
      });
      return false;
    }

    if (!this.state.selectedMonth || !this.state.selectedYear) {
      this.setState({
        showSnackbar: true,
        snackbarMessage: formatMessage({
          id: 'app.monthlylogspage.noselectedmonthyear',
          defaultMessage: 'Error: Make sure that you select a month and year.',
        }),
        selectedTimeCheck: false,
      });
      return false;
    }

    if (this.state.selectedTime.getHours() > 11 || this.state.selectedTimeTwo.getHours() < 12) {
      this.setState({
        showSnackbar: true,
        snackbarMessage: formatMessage({
          id: 'app.monthlylogspage.timeselecterror',
          defaultMessage: 'Error: Make sure that you properly select a time in the AM and time in the PM.',
        }),
        selectedTimeCheck: false,
      });
      return false;
    }
    this.setState({
      selectedTimeCheck: true
    });
    return true;
   }

  _getPDF() {
    this._checkSelectedTime();
  }

Specifically, _getPDF() is assigned to the onClick prop of a button, like so (in the render() function):

          <RaisedButton
            label={
              <span>
                <FormattedMessage
                  id="app.monthlylogspage.downloadtemperaturepdf"
                  defaultMessage="Download PDF"
                />
              </span>
            }
            primary={true}
            onClick={this._getPDF}
            // href={this._getGeneratePdfLink()}
            icon={<DownloadIcon />}
          />

Whenever I click the button, I get an error saying that this._checkSelectedTime is not a function at the line in getPDF() (Uncaught Type error: this.checkSelectedTime is not a function).

I was just wondering if there could be any reason why. Other function calls in the same component work fine.

Thanks

1 Answers1

0

sound like a binding issue. try using an arrow function on _getPDF

 _getPDF = () => {
    this._checkSelectedTime();
  }
knicholas
  • 520
  • 4
  • 10