您好,我是新来的反应,并试图通过假商店 API 构建一个结帐系统,用户可以在其中查看购物车中的物品,增加物品的数量,减少物品的数量并清除购物车。到目前为止,我已经在主页上呈现了产品。但我不知道如何增加商品购物车和查看购物车中的商品。我知道这可能是一项简单的任务,但我不知道如何进行,并且我试图避免使用视频教程。请对如何进行的任何建议表示赞赏。
import React from 'react'
import './App.css';
import axios from 'axios'
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
products: [],
count: 0
}
}
componentDidMount(){
axios.get('https://fakestoreapi.com/products')
.then(response =>{
const products = response.data
this.setState({products})
console.log(products)
} )
.catch(err=> console.log(err))
}
incrementCart = () =>{
this.setState({count: this.state.count + 1})
}
render() {
return (
<div id="container">
{
this.state.products.map(product=>
<div id="product" key={product.id}>
<img className="img" src={product.image} alt="product-store"/>
<h1>{product.category}</h1>
<h4>{product.price}</h4>
<button onClick={this.incrementCart}>Add To Cart</button>
</div>
)
}
</div>
)
}
}
export default App
