redux 案例购物车

1,环境我不搭建了,直接写!

App.js

import React , {Component} from "react";

import {connect} from "react-redux";

import {bindActionCreators} from "redux";
import * as actions from "./actions.js";
import ShopItem from './ShopItem';

class App extends Component {



	calcTotal(){
		var sum = 0;
		var cart = this.props.cart;
		this.props.cart.forEach((item)=>{
			sum +=(item.price*item.amount)
		});
		return sum;
	}


	render(){
		return (
			<div>
				<p>
					{JSON.stringify(this.props.cart)}
				</p>
				<h1>购物车</h1>	
				{this.props.cart.map(function(item){
					return (<ShopItem key = {item.id} item = {item}/>)
				})}
				
				<hr/>
				<p>
					总价格:{this.calcTotal()} 
				</p>
			</div>	
		);
	}
}

export default connect(
	(state) => {
		return {
			cart: state.cart		
		}
	},
	(dispatch) => {
		return {
		 actions : bindActionCreators(actions,dispatch)
		}
	}
)(App);

2, reducer.js




export default (state , action) => {


	if(state == undefined){
		state = {

			cart:[
				{
					id:1001,
					price:1000,
					amount:10,
					name:'apple'
				},
				{
					id:1002,
					price:2000,
					amount:1,
					name:'computer'	
				},
				{
					id:1003,
					price:100000,
					amount:1,
					name:'house'
				},
				{
					id:1004,
					price:1000000,
					amount:2,
					name:'people'
				}
			],
			total:14

		};
	}

	if(action.type=="ZENGJIA"){
		var id = action.id;
		console.log(id)
		state = {
			...state,
			cart:state.cart.map((item)=>{
				if(item.id !=id){
					return item;
				}else{
					item.amount = ++item.amount
					return item;
				}
			}),
			total :++state.total
		}
	}else if(action.type=="JIANSHAO"){
	var id = action.id;
		state = {
			...state,
			cart:state.cart.map((item)=>{
				if(item.id !=id){
					return item;
				}else{
					item.amount = --item.amount
					if(item.amount<0){
						item.amount = 0;
					}
					return item;
				}
			}),
			total :--state.total
		}	
	}	


	return state;
}



3, actions.js


export const jianshao = (id) => {
	return {"type" : "JIANSHAO","id":id}
};


export const zengjia = (id)=>{
	return {"type" : "ZENGJIA","id":id}
};

4, main.js

import React from "react";
import {render} from "react-dom";
import {createStore} from 'redux';

import App from "./App.js";

import reducer from './reducer.js';
import {Provider} from 'react-redux';

let store = createStore(reducer);
render(
	<Provider store = {store}>
		<App></App> 	
	</Provider>
	, 
	document.getElementById("app-container")
);

ShopItem.js

import React from 'react';
import * as actions from './actions.js';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';

class ShopItem extends React.Component {

	addItem(id){
		console.log(id)
		this.props.actions.zengjia(id)
		
	}

	minusItem(id){
		console.log(id)
		this.props.actions.jianshao(id)
		
	}


	render(){
		return (
			<div>
					
					<ul>
					<li><span>名字</span> {this.props.item.name} </li>
					<li><span>价格</span> {this.props.item.price}  </li>
					<li><span>数量</span> {this.props.item.amount} </li>
					<li><button onClick = {(this.addItem).bind(this,this.props.item.id)}>+</button> {" "}<button onClick = {(this.minusItem).bind(this,this.props.item.id)}>-</button></li>
					<li><span>总价</span> {this.props.item.price*this.props.item.amount} </li>
					</ul>

			
			</div>	
		);
	}

}


export default  connect(
	(state)=>{
		return {
			cart:state.cart
		}
	},

	(dispatch) =>{
		return{
			actions:bindActionCreators(actions,dispatch)
		}
	}
)(ShopItem);

总结,其实啥也没有,就是redux 中数据的单向流动!

actions 发送指令, reducer.js 中处理并返回新的数据,引起视图更新!

redux 写起来很舒服!

 

我这个没啥界面,很 好理解,不会的伙伴,可 参考前面3 篇内容!实在不会,可直接下载视频学习!

链接地址:

React入门和企业级项目 9天 (视频,笔记,源码完整)

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redux 是一个 JavaScript 状态管理库,常用于 React 应用程序中。它可以方便地管理 React 应用程序中的状态,并使其易于维护。 以下是一个 Redux 的使用案例: 1. 安装 Redux 使用以下命令安装 Redux: ``` npm install redux ``` 2. 创建 Redux Store Redux 应用程序的核心是 Store。 Store 存储着应用程序的状态,并提供了一些方法来更新状态。 ``` import { createStore } from 'redux'; const initialState = { count: 0 }; function reducer(state = initialState, action) { switch(action.type) { case 'INCREMENT': return { count: state.count + 1 }; case 'DECREMENT': return { count: state.count - 1 }; default: return state; } } const store = createStore(reducer); ``` 上述代码中,我们创建了一个名为 `store` 的 Redux Store,并定义了一个名为 `reducer` 的函数来处理状态更新。初始状态为 `{ count: 0 }`。 3. 在应用程序中使用 Redux ``` import React from 'react'; import { connect } from 'react-redux'; function Counter(props) { return ( <div> <p>Count: {props.count}</p> <button onClick={props.increment}>+</button> <button onClick={props.decrement}>-</button> </div> ); } const mapStateToProps = state => { return { count: state.count }; }; const mapDispatchToProps = dispatch => { return { increment: () => dispatch({ type: 'INCREMENT' }), decrement: () => dispatch({ type: 'DECREMENT' }) }; }; export default connect(mapStateToProps, mapDispatchToProps)(Counter); ``` 上述代码中,我们使用 `connect` 函数将 `Counter` 组件与 Redux Store 连接。`mapStateToProps` 函数将我们需要的状态从 Store 中提取出来,并将其作为 `props` 传递给 `Counter` 组件。`mapDispatchToProps` 函数将我们需要的操作(也就是 dispatch 函数)作为 `props` 传递给 `Counter` 组件。这样,我们就可以在组件中通过 `props` 访问和更新状态了。 以上就是一个简单的 Redux 使用案例。当我们点击 `+` 或 `-` 按钮时,就会触发 `INCREMENT` 或 `DECREMENT` 操作,并更新状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值