【发布时间】:2019-06-09 17:28:09
【问题描述】:
我有一个反应应用程序,我从 Json 文件中获取产品详细信息。它们显示正确,增量 - 减量按钮效果很好。
所以在 index.js 中,三个 js 组件被称为 main.js 、 header.js 、footer.js。
Main 获取 json 文件,创建容器和行,然后调用 8 次(因为 Json 中有 8 个项目)product.js 和在 Product.js 中,有关产品和单个按钮的所有信息都显示在页面上。
这是我的问题: 让每个项目的数量乘以相关价格并在标题中添加总数量和总价格的最简单方法是什么?
索引
import React from "react";
import ReactDOM from "react-dom";
import Main from "./components/main";
import Footer from "./components/footer";
import Header from "./components/header";
import './index.css';
import 'bootstrap/dist/css/bootstrap.css';
ReactDOM.render(<Main />, document.getElementById("root"));
ReactDOM.render(<Header />, document.getElementById("header"));
ReactDOM.render(<Footer />, document.getElementById("footer"));
标题
import React, { Component } from "react";
class header extends Component {
state = {
totalPrice: 200,
totalQuantity:0
};
render() {
return (
<div>
<nav className="navbar navbar-expand-lg navbar-dark bg-info">
<a className="navbar-brand" href="#">
<img src="./logo.png" id="logo" alt="" />
</a>
<button
className="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNavDropdown"
aria-controls="navbarNavDropdown"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon" />
</button>
<div className="collapse navbar-collapse" id="navbarNavDropdown">
<ul className="navbar-nav">
<li className="nav-item active">
<a className="nav-link" href="#">
Home <span className="sr-only">(current)</span>
</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">
Features
</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">
Pricing
</a>
</li>
</ul>
</div>
<input className="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"></input>
<button className="btn btn-success m-2" type="submit">Search</button>
<h2><span className={this.getClass()}>
Total Quantity:
Total Price: {this.formatCount()}
</span></h2>
</nav>
</div>
);
}
getClass() {
let classes = "badge";
classes += this.state.totalPrice === 0 ? " badge-danger" : " badge-warning";
return classes;
}
formatCount() {
const { totalPrice } = this.state;
return totalPrice === 0 ? "Your Cart is Empty" : totalPrice+"€";
}
}
export default header;
主要
import React, { Component } from 'react';
import ProductInfo from '../plist.json';
import Product from './product'
class Products extends Component {
render() {
return (
<div className="container">
<div className="row ">
{ProductInfo.map(postDetail => <Product {...postDetail} />)}
</div>
</div>
)
}
}
export default Products
产品
import React, { Component } from 'react';
class Product extends Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
handleIncerement = () => {
this.setState({
count: this.state.count + 1
});
}
handleDecrement = () => {
if(this.state.count< 1){
this.setState({
count:0
});
}else {
this.setState({
count: this.state.count- 1
});
}
}
render() {
const { name, image, price, description } = this.props;
let totalQuantity= 0;
let totalPrice = 0;
totalQuantity += this.state.count;
totalPrice += this.state.count * {price};
console.log("Quantity:"+ totalQuantity);
console.log("Total Price:"+ totalPrice);
return (
<div className="col-md-4 ml-auto">
<img className="productpic" src={require(`./images/${image}`)} alt="Product" />
<h2 className="display-6"> <a href="{url}">{name}</a></h2>
<p className="h5 price">{price}</p>
<p className="info">{description}</p>
<div className="counter">
<button className="btn btn-info" onClick={this.handleIncerement}>+</button>
<div className="count">{this.state.count}</div>
<button className="btn btn-info" onClick={this.handleDecrement}>-</button>
</div>
</div>
);
}
}
export default Product
【问题讨论】:
-
我完全阅读了你的问题,我向你推荐这个帖子:stackoverflow.com/questions/21285923/…
-
你可以使用组件和道具。