简体   繁体   English

create-react-app中onClick的事件处理程序使TypeError不确定

[英]Event handler for onClick in create-react-app gives TypeError undefined

I am creating a project using create-react-app for the first time. 我是第一次使用create-react-app创建项目。 I am using react-bootstrap for UI. 我正在为UI使用react-bootstrap。 I have a simple App.js component 我有一个简单的App.js组件

import ListGroup from 'react-bootstrap/lib/ListGroup';
import ListGroupItem from 'react-bootstrap/lib/ListGroupItem';

class App extends Component {

   constructor(props) {
     super(props);
     this.handleClick = this.handleClick.bind(this);
   }

   handleClick(e) {
      this.setState({ clicked: true})
   }

   render() {
      {this.state.response.map(function(object) {
        return (
          <ListGroup>
            <ListGroupItem  onClick={  this.handleClick }>{object.name}
            </ListGroupItem>
          </ListGroup>
        );
      })}
   }
}

I keep getting an error 我不断收到错误消息

TypeError: Cannot read property 'handleClick' of undefined

Could anyone help please? 有人可以帮忙吗?

Use an arrow function for the map callback to preserve this binding to your class: 使用箭头功能的map回调保留this绑定到类:

  this.state.response.map((object) => {
    return (
      <ListGroup>
        <ListGroupItem  onClick={  this.handleClick }>{object.name}
        </ListGroupItem>
      </ListGroup>
    );
  })

You should also make your handler an arrow function to preserve this binding: 您还应该使处理程序成为箭头函数以保留this绑定:

   handleClick = (e) => {
      this.setState({ clicked: true})
   }

Array.prototype.map() takes a second argument to set what 'this'. Array.prototype.map()使用第二个参数来设置“ this”。 See "this" is undefined inside map function Reactjs 看到地图函数Reactjs内部未定义“ this”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个 onClick 事件处理程序在我的 create-react-app 中触发两次 - Why is this onClick event handler firing twice in my create-react-app setAttribute在create-react-app中无法与onClick一起使用 - setAttribute not working with onClick in create-react-app 执行create-react-app会出现错误 - executing create-react-app gives an error TypeError:无法解构“对象(…)(…)”的属性“setValues”,因为它未定义。 (反应/创建反应应用) - TypeError: Cannot destructure property 'setValues' of 'Object(…)(…)' as it is undefined. (react / create-react-app) npx create-react-app 客户端给出此错误“无法读取未定义的属性(读取&#39;isServer&#39;)” - npx create-react-app client gives this error "Cannot read properties of undefined (reading 'isServer')" create-react-app 错误无法解决:Uncaught TypeError: Cannot read properties of undefined (reading 'pathname') - create-react-app errors not able to solve: Uncaught TypeError: Cannot read properties of undefined (reading 'pathname') 未捕获的TypeError:运行create-react-app构建版本时,无法读取未定义错误的属性“mountComponent” - Uncaught TypeError: Cannot read property 'mountComponent' of undefined error when running create-react-app build version 在create-react-app中意外使用事件 - Unexpected usage of event in create-react-app npm create-react-app myapp 给出错误命令失败:create-react-app spawn EPERM - npm create-react-app myapp gives error Command failed: create-react-app spawn EPERM 我的带有 create-react-app 的电子应用程序出错 - My electron app with create-react-app gives error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM