我在代碼 Sandbox IDE 中工作,組件 ProductList 有問題,我嘗試在 App.js 中添加函數。我在組件 ProductList 中將函數作為道具傳遞,但出現錯誤,顯示 TypeError:this.props.changeCategory is not a function。我將代碼編寫為“ onClick={() => this.props.changeCategory(category)}”我在 App.js 中將這個函數描述為changeCategory = category => { this.setState({ currentCategory: category.categoryName }); };我的錯誤信息: TypeError_this2.props.changeCategory is not a functiononClick/src/components/ProductList.js:21:40 18 | <ListGroup> 19 | {this.state.categories.map(category => ( 20 | <ListGroupItem> 21 | onClick={() => this.props.changeCategory(category)} | ^ 22 | key={category.categoryId} 23 | > 24 | {" "}產品列表.jsimport React, { Component } from "react";import { ListGroup, ListGroupItem } from "reactstrap";import "../styles.css";class ProductList extends Component { state = { categories: [ { categoryId: 1, categoryName: "Beverages" }, { categoryId: 2, categoryName: "Sunny Side UP" }, { categoryId: 3, categoryName: "Whats sup" } ] }; render() { return ( <div> <h3> {this.props.uc} </h3> <ListGroup> {this.state.categories.map(category => ( <ListGroupItem onClick={() => this.props.changeCategory(category)} key={category.categoryId} > {" "} {category.categoryName}{" "} </ListGroupItem> ))} </ListGroup> <form className="f">{this.props.currentCategory}</form> </div> ); }}
如何修復 TypeError:this.props.changeCategory 不是
qq_遁去的一_1
2022-07-15 09:28:56