Skip to content Skip to sidebar Skip to footer

Uncaught (in Promise) Typeerror: Cannot Read Property 'handleclick' Of Undefined In Foreach Loop

I have the following code articles_list.jsx import React from 'react'; import './articles_list.css'; export default class ArticlesList extends React.Component { constructor(pro

Solution 1:

You forgot to bind the forEach function and hence the error since, this inside forEach function will refer to its own context where handleClick is not defined rather than the React Component, Either use .bind(this) like

 {this.state.articles.forEach( function(element, index) {
    console.log(element);
    teste.push(<divonClick={this.handleClick}className="articles-menu-item"key={index.toString()}>{element.title}</div>);
  }.bind(this))}

or Arrow function

 {this.state.articles.forEach( (element, index) => {
    console.log(element);
    teste.push(<divonClick={this.handleClick}className="articles-menu-item"key={index.toString()}>{element.title}</div>);
  })}

Post a Comment for "Uncaught (in Promise) Typeerror: Cannot Read Property 'handleclick' Of Undefined In Foreach Loop"