Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
Why is my javascript function undefined in this react class component?

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

 _getPDF = () => {
    this._checkSelectedTime();
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda