React实现购物车功能

这个简单的demo,功能是这样的:
最左侧是列表,点击列表项可查看详情,中间是详情,详情中有按钮,点击可加入购物车,最右侧是购物车,购物车自动更新总数和总价.
使用create-react-app创建.
代码如下:

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import Books from './Books';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import './styles/index.css';

ReactDOM.render(
  <Books />,
  document.getElementById('root')
);

Books.js:

import React, { Component } from 'react';

import BookList from './BookList';
import BookDetail from './BookDetail';
import Car from './Car';

class Books extends Component {
  constructor() {
    super()
    this.handleListClick = this.handleListClick.bind(this);
    this.handleAddToCar = this.handleAddToCar.bind(this);

    this.state = {
      currentBook: null,
      car: [],
      totalNum: 0,
      total: 0
    };
  }

  handleListClick(book) {
    this.setState({
      currentBook: book
    });
  }

  handleAddToCar(currentBook) {
    let totalNum = this.state.totalNum;
    let car = this.state.car;
    let total = this.state.total;

    let exist = false;

    if (car.length) {
      car.forEach(book => {
        if (book.id === currentBook.id) {
          book.number += 1;
          totalNum += 1;
          exist = true;
          this.setState({
            totalNum
          });
        }
      });
    }

    if (!exist) {
      car = car.concat(Object.assign({}, currentBook, {number:1}));
      totalNum += 1;
      this.setState({
          car,
          totalNum
      }); 
    }

    total = car.map(book => (book.price * book.number)).reduce((prev, cur) => prev + cur);
    this.setState({
      total
    });
  }

  render() {
    return (
      <div className='row'>
        <BookList books={this.props.books} listClick={this.handleListClick}/>
        <BookDetail currentBook={this.state.currentBook} addToCar={this.handleAddToCar}/>
        <Car {...this.state} />
      </div>
    );
  }
}

Books.defaultProps = {
    books: [
    {
      id: 1,
      category: 'CSS',
      title: 'CSS权威指南',
      author: 'Eric A. Meyer',
      price: 42
    },
    {
      id: 2,
      category: 'JS',
      title: 'JavaScript高级程序设计',
      author: 'Nicholas C.Zakas',
      price: 69
    },
    {
      id: 3,
      category: 'CSS',
      title: '精通CSS:高级Web标准解决方案',
      author: '巴德,科利森,莫尔',
      price: 25
    }
  ]
};

export default Books;

BookList.js:

import React from 'react';

const BookList = ({books, listClick}) => {
  return (
    <div className='col-md-2'>
      <h2>图书列表</h2>
        <ul>
          {books.map((book) => {
            return (
              <li key={book.id} onClick={() => listClick(book)}>
                {book.title}
              </li>
            )            
          })}
        </ul>
    </div>
  );
};

export default BookList;

BookDetail.js

import React from 'react';

const Bookdetail = ({currentBook, addToCar}) => {
  if (!currentBook) return <div className='col-md-4'><h2>选择图书</h2></div>
  return (
    <div className='col-md-4'>
      <h2>图书详情</h2>
      <h3>{currentBook.title}</h3>
      <p>作者:{currentBook.author}</p>
      <p>价格:{currentBook.price}</p>
      <p>编号:{currentBook.id}</p>
      <button onClick={() => addToCar(currentBook)}>
        放进购物车
      </button>
    </div>
  );
};

export default Bookdetail;

Car.js:

import React from 'react';

const Car = ({car, totalNum, total}) => {
  let result = car.length ? <p>共{totalNum}本 总价为{total}</p>: <p>购物车为空</p>;
  return (
    <div className='col-md-6'>
      <h2>购物车</h2>
      <ul>
        {
          car.map((book, index) => {
            return <li key={index}>
              {book.title} 价格为: {book.price} 已选择{book.number}本
            </li>
          })
        }
      </ul>
      {result}
    </div>
  );
};

export default Car;
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值