reactjs - 没有未使用的表达式问题 React
问题描述
我正在使用 React 制作电子商务网站。有一个ProductList.js
列出的代码Product.js
。我正在使用从context.js
. 的代码是context.js
-
state = {
products: [],
detailProduct,
};
getItem = (id) => {
let product = this.state.products.find((item) => item.id === id);
return product;
};
handleDetail = (id) => {
let product = this.getItem(id);
this.setState(() => {
return { detailProduct: product };
});
};
addToCart = (id) => {
console.log(`Hello from Add to Cart. Id is: ${id}`);
};
render() {
return (
<ProductContext.Provider
value={{
...this.state,
handleDetail: this.handleDetail,
addToCart: this.addToCart
}}
>
{this.props.children}
</ProductContext.Provider>
);
}
这肯定不是完整的代码。如果您需要完整的代码,我也可以提供。
Product.js
is-的代码
<ProductConsumer>
{(value) => {
<div className="p-5 img-container" onClick={() => value.handleDetail(id)}>
<Link to="/details">
<img src={img} className="card-img-top" alt="Product Image" />
</Link>
<button className={inCart ? "in-cart cart-btn" : "cart-btn"} disabled={inCart ? true : false} onClick={() => value.addToCart(id)}>
{inCart ? (
<p className="text-capitalize mb-0" disabled>
Already in cart
</p>
) : (
<i className="fas fa-cart-plus"></i>
)}
</button>
</div>
}}
</ProductConsumer>
我得到的错误 -
./src/components/Product.jsx
Line 13:15: Expected an assignment or function call and instead saw an expression no-unused-expressions
Search for the keywords to learn more about each error.
请帮我解决这个问题...
解决方案
{
您在消费者块中使用花括号。将这些更改为括号,如下所示:
<ProductConsumer>
{(value) => (
<div className="p-5 img-container" onClick={() => value.handleDetail(id)}>
<Link to="/details">
<img src={img} className="card-img-top" alt="Product Image" />
</Link>
<button className={inCart ? "in-cart cart-btn" : "cart-btn"} disabled={inCart ? true : false} onClick={() => value.addToCart(id)}>
{inCart ? (
<p className="text-capitalize mb-0" disabled>
Already in cart
</p>
) : (
<i className="fas fa-cart-plus"></i>
)}
</button>
</div>
)}
</ProductConsumer>
推荐阅读
- nginx - 为什么我从 Nginx 收到只读文件系统错误?
- r - lapply() 的各种转换 - R
- json - 无法从 Swish QR 代码 API 理解此 JSON 模式
- javascript - 无法读取上下文提供程序中的 useReducer 挂钩更新的状态
- node.js - 我如何使用 nodejs (puppeteer) 监控网络活动
- r - 如何从 R 中的 sqldf 输出中获取列的总和?
- javascript - Angular - 如何使用 InnerHtml 执行存储在字符串中的脚本
- vue.js - 如何使用多个选择按钮过滤 Vue.js 中的数组?
- c++ - 将函数 SetKeys 添加到 MFRC522 库
- azure - Azure App Service Deploy 返回 (403) Forbidden with IP 限制