React-Native使用Mobx实现购物车功能

在工作中,购物车场景非常常见。本文实现基于React-Native和Mobx实现两种购物车例子。

其中,后期会加入动画等其他。本期先实现基础功能

二:基于State实现购物车

1-:ShoppingCarPage.js

constructor(props) {
super(props);
this.state = {
allSelecte : data.isAllSelect,totalMoney : data.totalMoney,}

};

getMoney = (m) => {
this.state.totalMoney=this.state.totalMoney+m;
//this.state.totalMoney += m;
data.totalMoney = this.state.totalMoney;
this.setState({
totalMoney : this.state.totalMoney
});

let i = 0;
data.datas.map((item) => {
  if (item.isSelect != true) {
    i += 1;
  }
});
if (i == 0) {
  data.isAllSelect = true;
  this.setState({ allSelecte : true })
}
else {
  data.isAllSelect = false;
  this.setState({ allSelecte : false })
}

};

renderItem = (item) => {
return (
<ShoppingItemComponent
itemData={item}
money={this.getMoney}
/>
)
};

allSelect = () => {
data.totalMoney = 0;
data.isAllSelect = !data.isAllSelect;
this.state.totalMoney = 0;
DeviceEventEmitter.emit('allSelect',!this.state.allSelecte);
this.setState({ allSelecte : !this.state.allSelecte })
};

separatorView = () => {
return (
<View style={{ height : 10,backgroundColor : '#e9e9e9' }}/>
)
};

keyExtractor = (item) => item.name;

render() {
let { allSelecte,totalMoney } = this.state;
return (

this.renderItem(item)} keyExtractor={ this.keyExtractor } /> 全选 ¥{this.state.totalMoney} 去结算
  </View>
);

}
}

2-:ShoppingItemComponent.js

static propTypes = {
itemData : PropTypes.object.isRequired,money : PropTypes.func,};

static defaultProps = {
money : () => null,};

componentDidMount() {
this.subscription = DeviceEventEmitter.addListener('allSelect',(isSelAll) => {
this.props.itemData.isSelect = isSelAll;
this.setState({ isSel : isSelAll });
if (isSelAll) {
this.setMoney(this.state.money * this.state.selNum);
}

})

};

componentWillUnmount() {
this.subscription && this.subscription.remove();
};

constructor(props) {
super(props);
this.state = {
isSel : this.props.itemData.isSelect,selNum : this.props.itemData.count,money : this.props.itemData.money,name : this.props.itemData.name,description : this.props.itemData.description,img : this.props.itemData.img,}
};

itemSelect = (item) => {
this.setState({isSel :!this.state.isSel},()=>{
if (this.state.isSel) {
this.setMoney(this.state.money this.state.selNum)
}
else {
this.setMoney(-this.state.money
this.state.selNum)
}
});
};

itemIncrease = (i) => {
i++;
this.setState({selNum : i},()=>{
if (this.state.isSel) {
this.setMoney(this.state.money)
}else{
this.setState({isSel :true});
this.setMoney(this.state.money * this.state.selNum);
}
this.props.itemData.count = i;
});
};

itemReduce = (i) => {
if (i <= 1) {
if(this.state.isSel){
this.setState({isSel :!this.state.isSel});
this.setMoney(-this.state.money)
}
return;
}
i--;
this.setState({ selNum : i },()=>{
if (this.state.isSel) {
this.setMoney(-this.state.money)
}else{
this.setState({isSel :true});
this.setMoney(this.state.money * this.state.selNum);
}
this.props.itemData.count = i;
});

};

setMoney = (money) => {
if (this.props.money) {
this.props.money(money);
}
};

render() {
let { itemData } = this.props;
let { isSel,selNum,money,name,description,img } = this.state;
return (
<View style={ styles.container }>
<TouchableOpacity
style={{ marginLeft : 15 }}
onPress={() => this.itemSelect(itemData)}>
<Image source={isSel ?
require('./imgs/login_radio_selected.png')
: require('./imgs/login_radio_normall.png')}/>

{ name } { description } @H_404_48@¥{ money }
          <TouchableOpacity style={ styles.reduceStyle } onPress={() => this.itemReduce(selNum)}>
            <Text style={{ color : selNum <= 1 ? 'red' : 'black' } }>-</Text>
          </TouchableOpacity>
          <View style={ styles.numberViewStyle }>
            <Text style={ styles.numberStyle }>{ selNum }</Text>
          </View>
          <TouchableOpacity style={ styles.increaseStyle } onPress={() => this.itemIncrease(selNum)}>
            <Text>+</Text>
          </TouchableOpacity>
        </View>
      </View>
    </View>
  </View>
);

}
}

三:基于Mobx实现购物车

1-:MobxShoppingCarPage.js

constructor(props) {
super(props);
this.data = new MobxStore();
};

componentDidMount() {
this.data.replace(jsonData)
};

@action
allSelect = () => {
DeviceEventEmitter.emit('allSelect',!this.data.itemData.isAllSelect);
this.data.selectAll();
};

renderItem = (item) => {
return (
<MobxShopItemComponent itemData={ item } data={ this.data }/>
)
};

separatorView = () => {
return (
<View style={{ height : 10,backgroundColor : '#e9e9e9' }}/>
)
};

keyExtractor = (item) => item.name;

render() {
return (
<View style={ styles.container }>
<FlatList data={ this.data.itemData.datas }
ItemSeparatorComponent={ this.separatorView }
renderItem={ ({ item }) => this.renderItem(item) }
keyExtractor={ this.keyExtractor }
/>
<View style={ styles.tool }>
<View style={{ flex : 1,alignItems : 'center' }}>
<TouchableOpacity style={ styles.select } onPress={ this.allSelect }>
<Image source={ this.data.itemData.isAllSelect ?
require('../imgs/login_radio_selected.png')
: require('../imgs/login_radio_normall.png') }/>
<Text style={{ marginLeft : 3 }}>全选

¥{ this.data.itemData.totalMoney } 去结算 ); } }

2-:MobxShopItemComponent.js

static propTypes = {
itemData : PropTypes.object.isRequired,data : PropTypes.object.isRequired,};

constructor(props) {
super(props);
this.itemData = this.props.itemData;
}

componentDidMount() {
this.subscription = DeviceEventEmitter.addListener('allSelect',(isSelAll) => {
this.itemData.isSelect = isSelAll;
})
};

componentWillUnmount() {
this.subscription && this.subscription.remove();
};

@action
selectPress = () => {
this.itemData.isSelect = !this.itemData.isSelect;
let money = this.itemData.money * this.itemData.count;
if (this.itemData.isSelect) {
this.props.data.increase(money);
}
else {
this.props.data.reduce(money)
}
this.props.data.itemPress();
};

@action
increase = () => {
this.itemData.count += 1;
if (this.itemData.isSelect) {
this.props.data.increase(this.itemData.money);
}else{
this.itemData.isSelect = !this.itemData.isSelect;
this.props.data.increase(this.itemData.money * this.itemData.count);
}

};

@action
reduce = () => {
if (this.itemData.count <= 1) {
if(this.itemData.isSelect){
this.itemData.isSelect = !this.itemData.isSelect;
this.props.data.reduce(this.itemData.money);
}
return;
}
this.itemData.count -= 1;
if (this.itemData.isSelect) {
this.props.data.reduce(this.itemData.money);
}
};

render() {
return (
<View style={ styles.container }>
<TouchableOpacity
style={{ marginLeft : 15 }}
onPress={ this.selectPress }>
<Image source={ this.itemData.isSelect ?
require('../imgs/login_radio_selected.png')
: require('../imgs/login_radio_normall.png') }/>

{ this.itemData.name } { this.itemData.description } @H_404_48@¥{ this.itemData.money } - { this.itemData.count } + ); } };

3-:MobxStore.js

export default class MobxStore {
@observable
itemData = {}

//设置数据
replace = (data) => {
this.itemData = data;
}

//按下的反选
itemPress = () => {
let i = 0;
this.itemData.datas.map((item) => {
if (item.isSelect != true) {
i += 1;
}
});
if (i == 0) {
this.itemData.isAllSelect = true;
}
else {
this.itemData.isAllSelect = false;
}
}

//加
increase = (money) => {
this.itemData.totalMoney += money;
}

//减
reduce = (money) => {
this.itemData.totalMoney -= money;
}

//全选
selectAll = () => {
this.itemData.isAllSelect = !this.itemData.isAllSelect;
this.itemData.totalMoney = 0;
if (this.itemData.isAllSelect) {
for (let i = 0;
i < this.itemData.datas.length;
i++) {
this.itemData.totalMoney += this.itemData.datas[ i ].money * this.itemData.datas[ i ].count;
}
}
}
}

四:

1-:代码github地址:https://github.com/erhutime/React-Navigation-All/tree/master/All/jscode/shoppingcar/src

2-:下载完成后,修改index.ios.js:入口文件如下:

运行效果如下:

App);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

事件冒泡和事件捕获 起因:今天在封装一个bind函数的时候,发现el.addEventListener函数支持第三个参数...
js小数运算会出现精度问题 js number类型 JS 数字类型只有number类型,number类型相当于其他强类型语言...
什么是跨域 跨域 : 广义的跨域包含一下内容 : 1.资源跳转(链接跳转,重定向跳转,表单提交) 2.资源...
@ &quot;TOC&quot; 常见对base64的认知(不完全正确) 首先对base64常见的认知,也是须知的必须有...
搞懂:MVVM模式和Vue中的MVVM模式 MVVM MVVM : 的缩写,说都能直接说出来 :模型, :视图, :视图模...
首先我们需要一个html代码的框架如下: 我们的目的是实现ul中的内容进行横向的一点一点滚动。ul中的内容...