码迷,mamicode.com
首页 > 其他好文 > 详细

react购物车demo

时间:2018-08-16 00:55:31      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:||   cti   def   default   取数   pat   this   let   col   

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;

 

react购物车demo

标签:||   cti   def   default   取数   pat   this   let   col   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!