最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react通過組件拆分實(shí)現(xiàn)購物車界面詳解

 更新時(shí)間:2022年08月15日 10:27:52   作者:是張魚小丸子鴨  
這篇文章主要介紹了react通過組件拆分來實(shí)現(xiàn)購物車頁面的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

頁面組件拆分圖

功能點(diǎn)

  • 實(shí)現(xiàn)全選反選
  • 數(shù)量的增加和減少
  • 選中刪除,單獨(dú)刪除
  • 商品總價(jià)和商品總數(shù)量的變化

首先在根組件中把頁面的布局以及功能先實(shí)現(xiàn),然后再拆分組件,最后通過組件傳值進(jìn)行屬性和方法的傳遞

代碼展示

app.js組件

import axios from 'axios';
import React, { Component } from 'react';
import './App.css';
import Cartfoot from './Component/Cartfoot';
import CartList from './Component/CartList';
export default class App extends Component {
  constructor() {
    super()
    this.state = {
      list: [],
      checked:false
    }
    this.getCart()
  }
  getCart = async () => {
    let res = await axios.get('http://127.0.0.1:3002/getCart')
    console.log(res.data.data);
    let carts = res.data.data
    carts.map(item => {
     return item.checked = false
    })
    this.setState({
      list: carts
    })
  }
  // 全選
  qx=(e)=>{
    let {list}=this.state
    list.map(item=>item.checked=e.target.checked)
    this.setState({
      list:list,
      checked:e.target.checked
    })
  }
  // 反選
  fx=(index)=>{
    let {list}=this.state
    list[index].checked = !list[index].checked
    this.setState({
      list:list,
      checked:list.every(item=>item.checked)
    })
  }
  // 選中刪除
  checkDel=()=>{
    let {list}=this.state
    let delAll=list.filter(item=>!item.checked)
    this.setState({
      list:[...delAll]
    })
  }
  // 數(shù)量加減操作
  handlerNum=(index,type="jia")=>{
    let {list}=this.state
    type==='jia'?list[index].num++ :list[index].num--
    this.setState({
      list:list
    })
  }
  // 計(jì)算操作
  count=()=>{
    let total=0
    let nums=0
    let {list}=this.state
    list.forEach(item=>{
      if(item.checked){
        total+=item.num*item.prize
        nums+=item.num
      }
    })
    return [total,nums]
  }
  // 刪除按鈕
  Del=(index)=>{
    let {list}=this.state
    list.splice(index,1)
    this.setState({
      list:list
    })
  }
  render() {
    let { list ,checked} = this.state
    return (
      <div className='App'>
        <table className='table'>
          <thead>
            <tr>
              <th><input type="checkbox" checked={checked} onChange={this.qx}/></th>
              <th>ID</th>
              <th>名字</th>
              <th>圖片</th>
              <th>價(jià)格</th>
              <th>數(shù)量</th>
              <th>操作</th>
            </tr>
          </thead>
          <CartList list={list} fx={this.fx} qx={this.qx} handlerNum={this.handlerNum} Del={this.Del} checked={checked}></CartList>
          <Cartfoot count={this.count} checkDel={this.checkDel}></Cartfoot>
        </table>
      </div>
    )
  }
}

在app組件中,我們把所有的方法和請求到的數(shù)據(jù)接口寫在這里,然后再通過props屬性進(jìn)行組件間的通信

CartList.js組件

import React from 'react'
import CartItem from './CartItem'
export default function CartList(props) {
  return (
    // <div>
        <tbody>
          {
              props.list.map((item, index) => {
                return (
                  <CartItem {...props} item={item} index={index} key={index}/>
                )
              })
            }
        </tbody>
    // </div>
  )
}

上面的{...props}是因?yàn)榻M件在傳遞屬性時(shí),如果傳遞的時(shí)候是一個(gè)對象屬性,我們可以使用擴(kuò)展運(yùn)算符傳遞數(shù)據(jù)

Cartfoot.js組件

import React from 'react'
export default function Cartfoot(props) {
  return (
        <tfoot>
        <tr>
              <td colSpan={7}>
                商品總價(jià)格:<strong>{props.count()[0]}</strong>
                商品總數(shù)量:<strong>{props.count()[1]}</strong>
                <button onClick={props.checkDel}>選中刪除</button>
              </td>
        </tr>
        </tfoot>
  )
}

CartItem.js組件

import React from 'react'
import CartColltract from './CartColltract'
export default function CartItem(props) {
    let {item,index}=props
  return (
    <tr>
                    <td><input type="checkbox" checked={item.checked} onChange={()=>props.fx(index)}/></td>
                    <td>{item._id}</td>
                    <td>{item.name}</td>
                    <td><img src={item.phopo} alt="" /></td>
                    <td>{item.prize}</td>
                    <td>  
                    <CartColltract {...props} item={item} index={index}></CartColltract>
                     </td>
                    <td><button onClick={()=>props.Del(index)}>刪除</button></td>
                  </tr>
  )
}

CartColltract.js組件

import React from 'react'
export default function CartColltract(props) {
    let {index,item}=props
  return (
    <div>
         <button className='danger' disabled={item.num === 1} onClick={()=>props.handlerNum(index,'jian')}>-</button>
                      <input type="text" value={item.num} readOnly />
                      <button onClick={()=>props.handlerNum(index,'jia')}>+</button>
    </div>
  )
}

像我們上面組件那樣,組件之間層層嵌套,我們不僅可以使用普通父傳子,子傳父的組件通信方式進(jìn)行組件傳值,也可以使用context兄弟組件通信

到此這篇關(guān)于react通過組件拆分實(shí)現(xiàn)購物車界面詳解的文章就介紹到這了,更多相關(guān)react組件拆分內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React中redux的使用詳解

    React中redux的使用詳解

    Redux 是一個(gè)狀態(tài)管理庫,它可以幫助你管理應(yīng)用程序中的所有狀態(tài),Redux的核心概念之一是Store,它表示整個(gè)應(yīng)用程序的狀態(tài),這篇文章給大家介紹React中redux的使用,感興趣的朋友一起看看吧
    2023-12-12
  • React中如何處理承諾demo

    React中如何處理承諾demo

    這篇文章主要為大家介紹了React中如何處理承諾demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • React router動(dòng)態(tài)加載組件之適配器模式的應(yīng)用詳解

    React router動(dòng)態(tài)加載組件之適配器模式的應(yīng)用詳解

    這篇文章主要介紹了React router動(dòng)態(tài)加載組件之適配器模式的應(yīng)用 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • React報(bào)錯(cuò)map()?is?not?a?function詳析

    React報(bào)錯(cuò)map()?is?not?a?function詳析

    這篇文章主要介紹了React報(bào)錯(cuò)map()?is?not?a?function詳析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • React插槽使用方法

    React插槽使用方法

    本文主要介紹了React插槽使用方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • React的事件處理你了解嗎

    React的事件處理你了解嗎

    這篇文章主要為大家詳細(xì)介紹了React的事件處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • react使用antd的上傳組件實(shí)現(xiàn)文件表單一起提交功能(完整代碼)

    react使用antd的上傳組件實(shí)現(xiàn)文件表單一起提交功能(完整代碼)

    最近在做一個(gè)后臺(tái)管理項(xiàng)目,涉及到react相關(guān)知識(shí),項(xiàng)目需求需要在表單中帶附件提交,怎么實(shí)現(xiàn)這個(gè)功能呢?下面小編給大家?guī)砹藃eact使用antd的上傳組件實(shí)現(xiàn)文件表單一起提交功能,一起看看吧
    2021-06-06
  • React函數(shù)式組件的性能優(yōu)化思路詳解

    React函數(shù)式組件的性能優(yōu)化思路詳解

    這篇文章主要介紹了React函數(shù)式組件的性能優(yōu)化思路詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • 使用hooks寫React組件需要注意的5個(gè)地方

    使用hooks寫React組件需要注意的5個(gè)地方

    這篇文章主要介紹了使用hooks寫React組件需要注意的5個(gè)地方,幫助大家更好的理解和學(xué)習(xí)使用React組件,感興趣的朋友可以了解下
    2021-04-04
  • React根據(jù)配置生成路由的示例代碼

    React根據(jù)配置生成路由的示例代碼

    React路由看似只能由Route組件包裹組件的結(jié)構(gòu)來構(gòu)成,但是其實(shí)也可以通過編寫路由數(shù)組配置然后通過數(shù)組循環(huán)來生成Route組件包裹組件的結(jié)構(gòu),所以本文給大家介紹了React根據(jù)配置生成路由的方法,需要的朋友可以參考下
    2024-11-11

最新評論

随州市| 普兰县| 扶绥县| 长垣县| 黄浦区| 江津市| 阿勒泰市| 申扎县| 介休市| 保定市| 霍城县| 芮城县| 通海县| 临泉县| 徐州市| 会泽县| 通许县| 繁昌县| 兴山县| 繁峙县| 舞钢市| 长岭县| 苗栗市| 逊克县| 沙田区| 平和县| 璧山县| 阿克苏市| 淅川县| 都江堰市| 南通市| 临泉县| 邵阳市| 宁南县| 盐亭县| 遂平县| 宜章县| 株洲市| 宣武区| 呈贡县| 昌黎县|