Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

77
Views
Cannot Read Properties Of Undefined Reading handleCategory

I'm trying to handle simple click action , I'm using class based components and I binded the method and used "this" as per documentation but an error occurs sayin cannot read properties of handleCategory , here's my code

import React from 'react';
import classes from '../css/Landing.module.css';
import mainClasses from '../../MainCss/MainClasses.module.css';
import { LOAD_CATEGORIES } from '../../graphql/queries';
export default class Landing extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            categories: [],
            loading: true,
            category: null,
        };
        this.handleCategory = this.handleCategory.bind(this);
    }

    async componentDidMount() {
        const { client } = this.props;
        const { data } = await client.query({ query: LOAD_CATEGORIES });
        const { categories } = data;
        this.setState((prevState) => ({ ...prevState, categories: categories, loading: false, category: categories[0]?.name }));
    }

    handleCategory(e) {
        this.setState((prevState) => ({ ...prevState, category: e.target.value }));
    }

    renderCategory(category) {
        return (
            <option onClick={this.handleCategory} key={category.name} value={category.name}>
                {category.name}
            </option>
        );
    }

    render() {
        return (
            <div className={`${classes.root} ${mainClasses.container} ${mainClasses.column}`}>
                <div className={`${mainClasses.container} ${mainClasses.row}`}>
                    <label className={classes.categorylabel}>Choose Category</label>
                    {this.state.loading ? 'loading' : <select className={classes.categorymenu}>{this.state.categories.map(this.renderCategory)}</select>}
                </div>
                <div>{this.state.category}</div>
            </div>
        );
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Convert the handleCategory method to an arrow function:

handleCategory = (e) => {
    this.setState((prevState) => ({ ...prevState, category: e.target.value }));
}

By doing this you can remove the explicit bind from the constructor.
For more info read here.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!