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

92
Views
Iterating Object with for-in loop in reactjs

This is a Reactjs code. Here i am trying to iterate and printing the Object data. But, facing an error --> TypeError: Cannot read properties of undefined (reading 'state'). I want to know what i am doing wrong here.

import React, { Component } from 'react'

class App extends Component {

  state = {
    posts: {
      ASlot: true,
      BSlot: true,
      CLive: true,
      DRedirect: true
    }
  }

  Club() {
  for(const property in this.state.posts) {
    console.log('hello')
    // console.log(`${property}: ${this.state.posts[property]}`);
  }
}

  render() {
    return <div>
      <div style={{ display: 'flex', justifyContent: 'center' }}>
        <span> hey, i am left text </span>
        <button onClick={this.Club}> Click Club </button>
      </div>
    </div>;
  }
}

export default App;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

import React from "react";
import "./styles.css";

class App extends React.Component {
  state = {
    posts: {
      name: "naveen",
      mail: "naveen9@gmail.com"
    }
  };
  club() {
    if (this.state.posts) {
      for (const property in this.state.posts) {
        console.log(property);
      }
    }
  }
  render() {
    return (
      <div>
        <button onClick={() => this.club()}>click</button>
      </div>
    );
  }
}
export default App;

You can use a conditional class.and it is working for me you can check here https://codesandbox.io/embed/cool-cerf-fxh9u?fontsize=14&hidenavigation=1&theme=dark

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!