import {createStore} from ‘redux’//用于创建仓库
import {combineReducers} from ‘redux’//
const initialState = {//state数据
cart: [
{
product: ‘bread 700g’,
quantity: 2,
unitCost: 90
},
{
product: ‘milk 500ml’,
quantity: 1,
unitCost: 47
}
]
}


const ADD_TO_CART = ‘ADD_TO_CART’;
const cartReducer = function(state=initialState, action) {//数据与action提交类型
switch (action.type) {//判断提交
case ADD_TO_CART: {
return {
…state,
cart: […state.cart, action.payload]
}
};break;
    default:
return state;
}
}
function addToCart(product, quantity, unitCost) {//actioc提交方法 具有属性与形参
return {
type: ADD_TO_CART,
payload: { product, quantity, unitCost }
}
}
const allReducers = {//多个state数据集合
products: cartReducer}
let a=combineReducers(allReducers)//通过com管理
let store=createStore(a)//仓库
store.dispatch(addToCart(‘Coffee 500gm’, 1, 250));
store.dispatch(addToCart(‘Flour 1kg’, 2, 110));
store.dispatch(addToCart(‘Juice 2L’, 1, 250));//dispatch分发


总结一点
redux 需要先创建store 然后通过接收一个函数,这个函数有两个形参,
一个形参是state=数据对象,一个是action判断提交,
当store通过dispatch方法接收一个具有type与payload为属性的对象返回值函数,当执行的时候通过type属性与action.type判断,接收action.payload值 返回数据对象引起store改变。