index.js

import React from 'react'
import ReactDOM from 'react-dom'
import CartSimple from './CartSimple' const jsx = (
<div>
<h1>React Study</h1>
<CartSimple/>
</div>
);
//渲染组件
ReactDOM.render(jsx,document.getElementById("root"));

CartSimple组件

import React, { Component } from 'react'

function CartByMe(props) {
return (
<table>
<tbody>
{
props.cart.map((goods, i) => {
return (<tr key={i}>
<td >名称:{goods.tital} </td>
<td >总价格:{goods.price * goods.count} </td>
</tr>)
}) }
</tbody>
</table>
)
}
export default class CartSimple extends Component { constructor(props) {
super(props) this.state = {
goods: [],
price: "",
tital: '',
cart: []
}
}
componentDidMount() {
//发起请求
setTimeout(() => {
let goods = [
{ id: 1, tital: "React入门", price: 100 },
{ id: 2, tital: "React高级", price: 800 },
{ id: 3, tital: "React进阶", price: 600 },
{ id: 4, tital: "React精通", price: 200 },
];
this.setState({
goods
})
}, 1000);
}
handlePrice = e => { this.setState(
{
price: e.target.value
}
);
}
handleTital = (e) => {
this.setState({
tital: e.target.value
})
}
addNewGood = (e) => {
if (this.state.tital && this.state.price) {
let lenMax = this.state.goods.length;
this.setState({
goods: [...this.state.goods, { id: lenMax + 1, tital: this.state.tital, price: this.state.price }]
})
} }
addShop = (id) => {
const goods = this.state.goods.map((item) => {
if(item.id == id){
return item;
}
});
const good = goods.filter((item)=>{
if(item!=null){
return item;
}
}) const cartGoods = this.state.cart.find(v => v.tital === good[0].tital);
if (cartGoods) {
//已经在购物侧里面有了
const newCart = [...this.state.cart];
newCart.forEach((item) => {
if (item.id == id) {
item.count += 1;
}
})
this.setState({
cart: newCart
}) } else {
//第一次添加商品 this.setState({
cart: [...this.state.cart, {
active: true,
id: good[0].id,
tital: good[0].tital,
price: good[0].price,
count: 1
}]
})
} }
render() {
return (
<div>
<h1>购物侧</h1>
<div>
<p>
<label htmlFor="tital">名字</label>
<input type="text" id="tital" value={this.state.tital} onChange={this.handleTital} />
</p>
<p>
<label htmlFor="price">价格</label>
<input type="text" id="price" value={this.state.price} onChange={this.handlePrice} />
</p> <button onClick={this.addNewGood} value="添加">添加</button>
</div>
<ul>
{
this.state.goods.map((item) => {
return (
<li key={item.id}>
<span>名称:{item.tital}</span>
<span>价格:{item.price}</span>
<button onClick={() => this.addShop(item.id)} value="">添加购物侧</button>
</li>);
})
}
</ul>
<hr></hr>
<CartByMe cart={this.state.cart}></CartByMe>
</div>
)
}
}

效果图

React综合使用联系的更多相关文章

  1. react综合案例-todolist、localstorage缓存数据

    1.工具类storage.js var app ={ set(key,value){ localStorage.setItem(key,JSON.stringify(value)); }, get(k ...

  2. react第二十单元(react+react-router-dom+redux综合案例2)

    第二十单元(react+react-router-dom+redux综合案例2) #课程目标 #知识点 #授课思路 #案例和作业

  3. react第十九单元(react+react-router-dom+redux综合案例1)

    第十九单元(react+react-router-dom+redux综合案例1) #课程目标 复习 综合练习 实战能力 #知识点 react react-router redux #授课思路 #案例和 ...

  4. React Native 之TabBarIOS

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  5. React Native之 ScrollView介绍和使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  6. React Native 之 组件化开发

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  7. React的井字过三关(3)

    这是React井字棋项目的最后一篇笔记,记述AI实现. 一. 是开头都会说的原理 但凡懂一点围棋的人都知道"大场"这个概念,可以浅显地把它理解为布局时棋盘上各处的要点.棋谚&quo ...

  8. React Native常用组件Image使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  9. React Native 开发之 (06) JSX

    一 React 1 React定义 React的GitHub地址是 https://github.com/facebook/react.它的官方介绍是 A JavaScript Library for ...

  10. [译] Angular 2 VS. React: 血色将至

    Angular 2 VS. React: 血色将至 原文链接:https://medium.com/@housecor/angular-2-versus-react-there-will-be-blo ...

随机推荐

  1. Typora自动上传超级详细教程!!

    第一步检查环境变量 打开cmd 查看以下环境变量 需要软件: Typora PicGo gitee账号 配置node 配置git 第二步创建gitee仓库 设置仓库名直接创建,因为这里不能直接修改开源 ...

  2. docker学习笔记-常用镜像相关命令

    docker images # 1.使用 [root@iZbp13qr3mm4ucsjumrlgqZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED ...

  3. Chrome 浏览器缓存的问题

    浏览器缓存的问题 Chrome 在浏览器中设置: 1 Ctrl + Shift + Delete 2 Ctrl + F5 强制刷新 3 F12 打开控制台 ---> Network ---> ...

  4. 详细说明-CentOS7部署FastDFS+nginx模块(包含集群方式)

    软件下载 # 已经事先把所需软件下载好并上传到/usr/local/src目录了 https://github.com/happyfish100/libfastcommon/archive/V1.0. ...

  5. Beats:使用Elastic Stack对Redis监控

  6. 5.监控MySQL

    prometheus监控MySQL需要用到mysql_exporter. mysql_exporter 项目地址:https://github.com/prometheus/mysqld_export ...

  7. GitLab 之 Git LFS 大文件存储的配置

    转载自:https://cloud.tencent.com/developer/article/1010589 1.Git LFS 介绍 Git 大文件存储(Large File Storage,简称 ...

  8. docker搭建yapi接口文档系统、Idea中上传接口、在线调用

    一.前言 在我们后端开发中,必不可少的是接口的交接,有很多种方式,常见的就是swagger,不过这个侵入性太强了.还有就是接口文档的框架,比如今天小编带大家一起搭建的yapi,在公司还是挺常见的! 今 ...

  9. win7升级到win10系统后,node13升级为node16,node版本node-sass版本与不匹配,导致出现npm ERR! ERESOLVE could not resolve

    1. 错误npm ERR! code ERESOLVE 系统从win7升级到win10,之前的node版本是13.14.0,现在版本是16.17.1.正常的vue程序无法正常运行.从网上查询得知&qu ...

  10. 记一次 .NET 某电子病历 CPU 爆高分析

    一:背景 1.讲故事 前段时间有位朋友微信找到我,说他的程序出现了 CPU 爆高,帮忙看下程序到底出了什么情况?图就不上了,我们直接进入主题. 二:WinDbg 分析 1. CPU 真的爆高吗? 要确 ...