简体   繁体   English

购物车实施 React-Redux

[英]Shopping Cart implementation React-Redux

I am implementing a shop cart using react-redux.我正在使用 react-redux 实现购物车。 I got two reducers, 1.To fetch cart data from DB 2. To Carry out various cart operations.我有两个减速器,1.从数据库中获取购物车数据 2.执行各种购物车操作。

My doubt is after achieving data from DB through the first reducer, how will I access that data through the 2nd reducer in order to carry out different cart operations?我的疑问是在通过第一个 reducer 从 DB 获取数据后,我将如何通过第二个 reducer 访问该数据以执行不同的购物车操作?

Reducer 1 - Fetch Data from DB Reducer 1 - 从数据库中获取数据

const initialState={
    loading:false,
    items:[], 
    error:false
}

const CartFetch=(state=initialState,action)=>{
    switch(action.type){
        
        case FETCHDATA : return { 
            ...state,loading:true ,error:false
        };

        case FETCHSUCCESS: return { 
            ...state,loading:false,
            items:[...action.payload]
        };

        case FETCHERROR : return { 
            ...state,loading:false,error:true
        };

        default: return state;
    }
}

Fetch Actions获取操作

const fetch=()=>{
    return {
        type:FETCHDATA
    }
}

const success=(user)=>{
    return {
        type:FETCHSUCCESS,
        payload:user
    }
}

const error=()=>{
    return {
        type:FETCHERROR
    }
}

const fetchCartData=()=>{

    const {id}=getCurrentUser();

    return (dispatch)=>{
        dispatch(fetch());
        axios.get(`${api.userOperations}/cart/${id}`,{
            headers:{'Authorization': getJwt()}
        }).then(({data})=>{
            dispatch(success(data));
        }).catch(()=>{
            dispatch(error())
        })
    }
}

Reducer 2 - Cart Operations减速机 2 - 推车操作

const CartHandle=(state= ..?.. ,action)=>{

        switch(action.type){
            
            case ADD_TO_CART :
            return {
                ......
            };

            case INCREMENT_CART : return { 
                ....
            };

            case DECREMENT_CART: return { 
                ......
            };

            case REMOVE_FROM_CART : return { 
                .....
            };

            default: return state;
        }
    }
}

Here in Reducer 2 how will I access the pass the data which I fetched in Reducer 1?在 Reducer 2 中,我将如何访问我在 Reducer 1 中获取的数据? Or easy there any better way of implementing what I m trying to?或者容易有更好的方法来实现我想要的?

Combine Reducers组合减速机

const allReducer=combineReducers({
    Cart:CartFetch,
    CartOperations: CartHandle
});

Store店铺

const countStore=createStore(allReducer,applyMiddleware(thunk));

<Provide store={store}>

...App.js...

</Provider>

Issue问题

It seems you don't quite fully understand what a reducer represents.看来你还没有完全理解 reducer 代表什么。 Each reducer represents a specific "chunk" or slice of state.每个 reducer 代表 state 的特定“块”或切片。 No two reducers function/operate on the same slice of state.没有两个减速器在 state 的同一片上运行/操作。 In other words, two separate reducers equals two separate slices of state.换句话说,两个独立的 reducer 等于 state 的两个独立切片。

Solution解决方案

Since a reducer represents a specific slice of state it needs to handle all the actions that are associated with that slice.由于 reducer 代表 state 的特定切片,因此它需要处理与该切片关联的所有操作。 You just need to merge your second reducer into the first on so it fully manages the cart state.您只需要将第二个减速器合并到第一个减速器中,它就可以完全管理购物车 state。

const initialState = {
  loading: false,
  items: [],
  error: false
};

const cartReducer = (state = initialState, action) => {
  switch (action.type) {
    case FETCHDATA:
      return {
        ...state,
        loading: true,
        error: false
      };

    case FETCHSUCCESS:
      return {
        ...state,
        loading: false,
        items: [...action.payload]
      };

    case FETCHERROR:
      return {
        ...state,
        loading: false,
        error: true
      };

    case ADD_TO_CART:
      return {
        // ......
      };

    case INCREMENT_CART:
      return {
        // ....
      };

    case DECREMENT_CART:
      return {
        // ......
      };

    case REMOVE_FROM_CART:
      return {
        // .....
      };

    default:
      return state;
  }
};

Create your root reducer, each combined reducer represents a slice of state.创建你的根减速器,每个组合减速器代表 state 的一片。

const allReducer = combineReducers({
  // ... other state slice reducers
  cart: cartReducer,
  // ... other state slice reducers
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM