const data = [
{
id: 0,
a: 'a',
b: 'b'
},
{
id: 1,
a: 'c',
b: 'd'
},
{
id: 2,
a: 'e'
}
]
addTotals = () => {
let subTotal = 0;
this.state.cart.map((item: Products) => (subTotal += item.total)); // error
const total = subTotal;
this.setState(() => {
return {
cartTotal: total
};
});
};
addToCart = (id: number) => {
let tempProduct: Products[] = [...this.state.products];
const item = this.getItem(id)
const index = tempProduct.indexOf(item as Products);
const product = tempProduct[index];
product.inCart = true;
product.count = 1;
const price = product.price;
product.total = price;
this.setState(
() => {
return {
products: tempProduct,
cart: [...this.state.cart, product]
}
}
,
() => {
this.addTotals();
}
);
};
increment = (id: number) => {
let tempCart = [...this.state.cart];
const selectedProduct = tempCart.find(item => item.id === id);
const index = tempCart.indexOf(selectedProduct as Products);
const product = tempCart[index];
product.count = product.count + 1;
product.total = product.count * product.price;
this.setState(
() => {
return {
cart: [...tempCart]
};
},
() => {
this.addTotals();
}
);
};
class CustomError extends Error {
constructor(foo = 'bar', ...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, CustomError);
}
this.name = 'CustomError';
// Custom debugging information
this.foo = foo;
this.date = new Date();
}
}
try {
throw new CustomError('baz', 'bazMessage');
} catch(e){
console.error(e.name); //CustomError
console.error(e.foo); //baz
console.error(e.message); //bazMessage
console.error(e.stack); //stacktrace
}
import React from 'react';
import axios from 'axios';
import {Table} from "reactstrap";
class Newpage extends React.Component {
constructor(props){
super(props);
this.state={
token: '',
items:[]
}
}
componentDidMount(){
axios.post(`https://gentle-escarpment-19443.herokuapp.com/v1/users/auth`,
{
email:"user1@email.com",
password: '!password!'
},
{headers: {
'Content-Type': 'application/json'
}})
.then(res => {
this.setState({
token: res.data.access_token
})
});
axios.get("https://gentle-escarpment-19443.herokuapp.com/v1/articles",{
headers: {
'Authorization': 'Bearer ' + this.state.token }}
)
.then((response)=>{
this.setState({
items: response.data
})
});
}
render() {
console.log(this.state.token)
let items = this.state.items.map((item) => {
return(
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.name}</td>
<td>{item.price}</td>
</tr>
)
});
return (
<div>
{this.state.token}
<Table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Price</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{items}
</tbody>
</Table>
</div>
)
}
}
export default Newpage;
<img src="https://habrastorage.org/webt/5d/0b/71/5d0b71de73aa5833410172.png" alt="image"/>