亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

TypeError:無法讀取未定義 React JS 的屬性“handleClick”

TypeError:無法讀取未定義 React JS 的屬性“handleClick”

米琪卡哇伊 2022-05-26 17:11:13
我是 React 的新手,我似乎無法弄清楚這一點。我正在嘗試在線練習,當我嘗試在我的子組件上傳遞事件時,我收到錯誤“無法讀取未定義的屬性'handleClick'”錯誤。這是我的代碼。import React from "react";import TodoItems from "./TodoItems";import TodoComponent from "./TodoComponent";class App extends React.Component {  constructor() {    super();    this.state = {      todoanItem: TodoItems    };    this.handleClick = this.handleClick.bind(this);  }  handleClick(id) {    this.setState(function(state) {      const newstate = state.todoanItem.map(function(todo) {        if (todo.id === id) {          todo.complete = !todo.complete;        }        return todo;      });      return {        todoanItem: newstate      };    });  }  render() {    const theItems = this.state.todoanItem.map(function(item) {      return (        <TodoComponent          key={item.id}          item={item}          handleClick={this.handleClick}        />      );    });    return <div>{theItems}</div>;  }}export default App;import React from "react";class TodoComponent extends React.Component {  render() {    return (      <div className="todo-items">        <input          type="checkbox"          checked={this.props.item.completed}          onChange={event => this.props.handleClick(this.props.item.id)}        />        <p>{this.props.item.todo}</p>      </div>    );  }}export default TodoComponent;
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

您需要在函數中使用箭頭render函數App:


const theItems = this.state.todoanItem.map((item) => { // Here

  return (

    <TodoComponent

      key={item.id}

      item={item}

      handleClick={this.handleClick}

    />

  );

});

此外,在以下位置添加一個構造函數TodoComponent:


constructor(props) { 

    super(props);

}


查看完整回答
反對 回復 2022-05-26
  • 1 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號