I have a react app where I fetch Products details from a Json file. They are displayed properly and Increment-Decrement buttons work well.
So in index.js three js components are called main.js , header.js ,footer.js.
Main gets the json file create the container and row then calls 8 times(because there 8 items in Json) product.js and In Product.js all info about product and individual buttons are displayed on page.
Here is my question: What is the easiest way to get each items quantity multiply with relate price and add Total quantity and Total price in header?
index
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"));
Header
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;
Main
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
Product
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