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. [机器学习]-分类问题常用评价指标、混淆矩阵及ROC曲线绘制方法

    分类问题 分类问题是人工智能领域中最常见的一类问题之一,掌握合适的评价指标,对模型进行恰当的评价,是至关重要的. 同样地,分割问题是像素级别的分类,除了mAcc.mIoU之外,也可以采用分类问题的一些 ...

  2. [Python]-pydicom模块处理DICOM数据

    在处理医疗数据时,经常要跟DICOM文件打交道.在使用Python处理时,不得不提常用的pydicom模块. import pydicom DICOM文件读取 pydicom.read_file()读 ...

  3. k8s实际操作中的小知识点

    1.批量执行yaml文件 # 把所有要执行的yaml文件放在同一个目录下,并且切换到这个目录下 kubectl apply -f . 2.利用pod的亲和和反亲和功能把pod调度到不同的node上 亲 ...

  4. Elasticsearch:significant terms aggregation

    在本文中,我们将重点关注significant terms和significant text聚合.这些聚合旨在搜索数据集中有趣和/或不寻常的术语,这些术语可以告诉您有关数据的隐藏属性的更多信息.此功能 ...

  5. Linux下登陆MySQL时遇到报错"RROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) "

    前言 作者在2021-07-21时遇到 linux下登陆MySQL时遇到报错"RROR 1045 (28000): Access denied for user 'root'@'localh ...

  6. 15_abstract,static,final

    一. abstract 1. 抽象类 被abstract修饰的类,称为抽象类 抽象类意为不够完整的类.不够具体的类 抽象类对象无法独立存在,即不能new对象,但可以声明引用 作用: 可被子类继承,提供 ...

  7. bfs与dfs基础

    bfs像二叉树的层序遍历 像这个图走bfs就{1, 2, 3, 4, 5, 6, 7, 8}这样走: dfs就{1, 2, 5, 6, 3, 7, 8, 4}. bfs与queue相结合,走到哪就把哪 ...

  8. 220722 T4 求和 /P4587 [FJOI2016]神秘数 (主席树)

    好久没打主席树了,都忘了怎么用了...... 假设我们选了一些数能构成[0,x]范围内的所有值,下一个要加的数是k(k<=x+1),那么可以取到[0,x+k]内的所有取值,所以有一种做法: 对于 ...

  9. Qt class 前置声明

    转载:https://www.cnblogs.com/ycbeginner/p/9403976.html 在Qt开发项目中,经常会用到各种库,但是一般在.h文件中进行某类型变量定义时,都会对其类型的c ...

  10. RNN自学理解(一)

    RNN对具有序列特性的数据非常有效,它能挖掘数据中的时序信息以及语义信息,利用了RNN的这种能力,使深度学习模型在解决语音识别.语言模型.机器翻译以及时序分析等NLP领域的问题时有所突破. 参考文献1 ...