React实现简易购物车项目(删除,数量加减,总价等)

才学react不久,来分享一个简易的demo,并且当购物车为空的时候展示购物车为空

先定义一个价格格式化的函数,把价格转成:¥+并且保留两位小数

function formatPrice(price){
  if(typeof price !=="number"){
    price = Number("aaa") || 0
  }
  return "¥"+ price.toFixed(2)
}

再定义一个类组件,state里面这样的数据

class Movie extends React.Component{
constructor(){
super()
this.state={
  books:[
  {id:1,name:"《算法导论》",datas:'2006-9',price:25,numbers:1},
  {id:2,name:"《编程艺术》",datas:'2006-9',price:45,numbers:1},
  {id:3,name:"《代码大全》",datas:'2006-9',price:15,numbers:1},
  {id:4,name:"《天演论》",datas:'2006-9',price:115,numbers:1},
], 
}
}

增加,减少功能

<button onClick={()=>this.changeBookCount(index,-1)}
disabled={item.numbers ==1}>-</button>
<span>{item.numbers}</span>
<button onClick={()=>this.changeBookCount(index,1)}>+</button>

changeBookCount(index,count){
    const newBooks =[...this.state.books]
    newBooks[index].numbers +=count
    this.setState({
      books:newBooks
    })
  }

删除功能

<button onClick={()=>this.removeItem(index)}>移除</button>

removeItem(index){
   this.setState({
     books:this.state.books.filter((item,indey)=>index !=indey)
   })
  }

总价

  <h2>总价格:{this.getTotalprice()}</h2>

  getTotalprice(){
let totalPrice = this.state.books.reduce((pre,item)=>{
  return pre+item.price * item.numbers
},0)
return formatPrice(totalPrice)

}

最后附上所有代码

function formatPrice(price){
  if(typeof price !=="number"){
    price = Number("aaa") || 0
  }
  return "¥"+ price.toFixed(2)
}

class Movie extends React.Component{
  constructor(){
    super()
    this.state={
      books:[
      {id:1,name:"《算法导论》",datas:'2006-9',price:25,numbers:1},
      {id:2,name:"《编程艺术》",datas:'2006-9',price:45,numbers:1},
      {id:3,name:"《代码大全》",datas:'2006-9',price:15,numbers:1},
      {id:4,name:"《天演论》",datas:'2006-9',price:115,numbers:1},
    ], 
    }
  }

  renderBooks(){
    return( 
    <div>  
       <table>
       <thead>
       <tr>
       <th></th>
       <th>书籍名称</th>
       <th>出版日期</th>
       <th>价格</th>
       <th>购买数量</th>
       <th>操作</th>
       </tr>
      </thead>
      <tbody>
       {
         this.state.books.map((item,index)=>{
           return (
            <tr>
           <td>{index+1}</td>
           <td>{item.name}</td>
           <td>{item.datas}</td>
           <td>{formatPrice(item.price)}</td>
           <td>
           <button onClick={()=>this.changeBookCount(index,-1)}
           disabled={item.numbers ==1}>-</button>
           <span>{item.numbers}</span>
           <button onClick={()=>this.changeBookCount(index,1)}>+</button>
          </td>
           <td><button onClick={()=>this.removeItem(index)}>移除</button></td>
          </tr>)
         })
       }
      </tbody>
       </table>
       <h2>总价格:{this.getTotalprice()}</h2>
      </div>)
  }

  renderNone(){
    return <h2>购物车为空</h2>
  }
  render() {
    const {books} = this.state
    return books.length ==0?this.renderNone():this.renderBooks()
  }
  changeBookCount(index,count){
    const newBooks =[...this.state.books]
    newBooks[index].numbers +=count
    this.setState({
      books:newBooks
    })
  }
  removeItem(index){
   this.setState({
     books:this.state.books.filter((item,indey)=>index !=indey)
   })
  }
  // getTotalprice(){
  //   1.第一种
  //   let totalPrice = 0;
  //   for(let i of this.state.books){
  //     totalPrice +=i.price * i.numbers
  //   }
  //   return  formatPrice(totalPrice)
  // }
  //2.第二种
  getTotalprice(){
    let totalPrice = this.state.books.reduce((pre,item)=>{
      return pre+item.price * item.numbers
    },0)
    return formatPrice(totalPrice)
  }
}
ReactDOM.render(
  <Movie/>,document.getElementById('app')
)
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
React实现数量并计算总价的功能,可以采用以下步骤: 1. 在组件的 state 中定义商品数量总价的初始值,例如: ``` state = { quantity: 1, totalPrice: 10 } ``` 2. 在 JSX 中渲染商品数量总价的显示,例如: ``` <div> <p>数量:{this.state.quantity}</p> <p>总价:{this.state.totalPrice}</p> </div> ``` 3. 编写数量的方法,例如: ``` handleIncrement = () => { const { quantity, totalPrice } = this.state; this.setState({ quantity: quantity + 1, totalPrice: totalPrice + 10 }); } handleDecrement = () => { const { quantity, totalPrice } = this.state; if (quantity > 1) { this.setState({ quantity: quantity - 1, totalPrice: totalPrice - 10 }); } } ``` 4. 在 JSX 中添数量的按钮,并绑定对应的方法,例如: ``` <div> <button onClick={this.handleDecrement}>-</button> <button onClick={this.handleIncrement}>+</button> </div> ``` 5. 在商品的数据中添价格信息,并在计算总价时使用该价格信息,例如: ``` const products = [ { id: 1, name: '商品1', price: 10 }, { id: 2, name: '商品2', price: 20 } ]; handleIncrement = () => { const { quantity, totalPrice } = this.state; const productPrice = products[0].price; // 假设选中了第一个商品 this.setState({ quantity: quantity + 1, totalPrice: totalPrice + productPrice }); } handleDecrement = () => { const { quantity, totalPrice } = this.state; const productPrice = products[0].price; // 假设选中了第一个商品 if (quantity > 1) { this.setState({ quantity: quantity - 1, totalPrice: totalPrice - productPrice }); } } ``` 这样,就可以根据选中的商品实现数量并计算总价的功能了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值