react购物车demo

import React, { Component } from ‘react‘;
import ‘./App.css‘;
import {connect} from ‘react-redux‘;
import {bindActionCreators} from ‘redux‘;
import action from ‘./shopcar/reduce/action‘
// ui 组件 只取数据
class App extends Component {
  constructor(props){
    super(props);
    this.addfriut = this.addfriut.bind(this);
    this.allprice = this.allprice.bind(this);
    this.state = {
      n: 0
    }
  };
  addfriut(){
    this.props.add(this.goods.value,this.price.value);
    this.goods.value=‘‘;
    this.price.value =‘‘
    console.log(this.props);
  };
  allprice(){
    console.log(this.props);
    let {list} = this.props.myReducer;
    let len = list.length;
    let n = 0;
    for(let i=0; i<len;i++){
      n+= list[i].price*this.refs[‘goods‘+i].value||0
    }
    this.setState({
        n
    })
  }
  render() {
   let {list} = this.props.myReducer;
    return (
      <div className="App">
        名字:<input type="text" ref={goods=>this.goods=goods}/>
        <br/>
        价格:<input type="text" ref={price=>this.price=price}/>
        <button onClick={this.addfriut}>addfriut</button>
        <button onClick={this.allprice}>计算价格</button>
      <ul>
          {list.map((item)=>{
            return (
              <li key={item.id}>
                <span>
                水果:{item.goods}  价格:{item.price}
                </span>
                <input type="text" ref={‘goods‘+item.id}/>
              </li>
            )
        })}
        {this.state.n}
      </ul>
      </div>
    );
  }
}
// // 实现计算属性的操作 类似vuex getters
let mapStateToProps = (state)=>{

  return state
};
// // 将dispatch传进去省区了actions 里的方法去 触发action的内置对象
let mapDispatchToProps = (dispatch)=>bindActionCreators(action,dispatch)
export default connect(mapStateToProps,mapDispatchToProps)(App);
// export default App;

原文地址:https://www.cnblogs.com/l8l8/p/9484334.html

时间: 2024-08-30 17:26:29

react购物车demo的相关文章

购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证

原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP.NET Web API的购物车案例,非常精彩,这里这里记录下对此项目的理解. 文章:http://chsakell.com/2015/01/31/angularjs-feat-web-api/http://chsakell.com/2015/03/07/angularjs-feat-web-api-

购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session

原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session chsakell分享了前端使用AngularJS,后端使用ASP.NET Web API的购物车案例,非常精彩,这里这里记录下对此项目的理解. 文章:http://chsakell.com/2015/01/31/angularjs-feat-web-api/http://chsakell.com/2015/03/07/angularjs-feat-web-api-en

购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端

原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端 chsakell分享了前端使用AngularJS,后端使用ASP.NET Web API的购物车案例,非常精彩,这里这里记录下对此项目的理解. 文章:http://chsakell.com/2015/01/31/angularjs-feat-web-api/http://chsakell.com/2015/03/07/angularjs-feat-web-api-enable-session-

谈谈我对前端组件化中“组件”的理解,顺带写个Vue与React的demo

前言 前端已经过了单兵作战的时代了,现在一个稍微复杂一点的项目都需要几个人协同开发,一个战略级别的APP的话分工会更细,比如携程: 携程app = 机票频道 + 酒店频道 + 旅游频道 + ...... 每个频道有独立的团队去维护这些代码,具体到某一个频道的话有会由数十个不等的页面组成,在各个页面开发过程中,会产生很多重复的功能,比如弹出层提示框,像这种纯粹非业务的UI,便成了我们所谓的UI组件,最初的前端组件也就仅仅指的是UI组件. 而由于移动端的兴起,前端页面的逻辑已经变得很重了,一个页面的

学习react入门demo的总结。

在github上找到react入门学习比较好的demo,下面是那个链接: https://github.com/ruanyf/react-demos 然后接下来是每个demo的学习笔记: demo1: <body> <div id="example"></div> <script type="text/jsx"> //jsx类型可html和javasript一起写,遇到 HTML 标签,就用 HTML 规则解析:遇到代

react新手demo——TodoList

react-todolist.gif 一: 写在文章开头 今天我们就使用 react 来实现一个简易版的 todolist ,我们可以使用这个 demo 进行 list 的增删改差,实际效果如上图所示.大家可以clone下来查看:react-todolist 这篇文章我们就不使用 redux,因为这个 demo 本身比较简单,不需要通过 redux 来管理我们的状态. redux中也有非常有名的一句话叫做: "如果你不知道是否需要 Redux,那就是不需要它." 我们废话不多说,直接进

Android仿淘宝购物车demo

夏的热情渐渐退去,秋如期而至,丰收的季节,小编继续着实习之路,走着走着,就走到了购物车,逛过淘宝或者是京东的小伙伴都知道购物车里面的宝贝可不止一件,对于爱购物的姑娘来说,购物车里面的商品恐怕是爆满,添加不进去了,以前逛淘宝的时候,小编没有想过要怎么样实现购物车,就知道在哪儿一个劲儿的逛,但是现在不一样了,小编做为一个开发者,想的就是该如何实现,捣鼓了两天的时间,用listview来实现,已经有模有样了,现在小编就来简单的总结一下实现购物车的心路历程,帮助有需要的小伙伴,欢迎小伙伴们留言交流. 首

React Native DEMO for Android

Demo1: 主要知识:navigator,fecth 地址:https://github.com/hongguangKim/ReactNativeDEMO1 Demo2: 主要知识:navigator,fecth,react-native-tab-navigator 地址:https://github.com/hongguangKim/ReactNativeDEMO2 Demo3: 主要知识:react-native-swiper,Animated 地址:https://github.com/

python购物车demo

product_list = [        ('Iphone',11800),        ('Mac Pro',13800),        ('BMW CAR',480000),        ('Watch',10600),        ('Coffee',31),        ('fatyao Python book',35),]shopping_list = []salary = input("Input your salary:")if salary.isdigi