Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Reducer-React从阵列中移除所有项目_Javascript_Reactjs_React Redux - Fatal编程技术网

Javascript 使用Reducer-React从阵列中移除所有项目

Javascript 使用Reducer-React从阵列中移除所有项目,javascript,reactjs,react-redux,Javascript,Reactjs,React Redux,下面是我用来从我的数组(购物车)中移除物品的减缩器的结构。在下面的代码中,我一次只能删除一个项目。当我点击触发减速器的移除按钮或图标时,如何移除购物车上的所有项目 附言:初学者与反应 Reducer.JS export default (state = INIT_STATE, action) => { switch (action.type) { case ON_DELETE_ITEM_FROM_CART: let index = stat

下面是我用来从我的数组(购物车)中移除物品的减缩器的结构。在下面的代码中,我一次只能删除一个
项目
。当我点击触发减速器的移除按钮或图标时,如何移除购物车上的所有项目

附言:初学者与反应

Reducer.JS

export default (state = INIT_STATE, action) => {
    switch (action.type) {

        case ON_DELETE_ITEM_FROM_CART:
            let index = state.cart.indexOf(action.payload)
            return update(state, {
                cart: {
                    $splice: [[index, 1]]
                }
            });
   }

在这里,您可以从我的减速机中清空您的完整购物车代码

   const initialState = {
  items: {},
  totalAmount: 0,
};
const cartItems = (state = initialState, action) => {
  switch (action.type) {


case CLEAR_CART:
        
          return  state={ items: {}, totalAmount: 0, }

在这里,您可以从我的减速机中清空您的完整购物车代码

   const initialState = {
  items: {},
  totalAmount: 0,
};
const cartItems = (state = initialState, action) => {
  switch (action.type) {


case CLEAR_CART:
        
          return  state={ items: {}, totalAmount: 0, }

为什么不在reducer中分配空数组购物车=
[]
?@DennisVash?为什么不在reducer中分配空数组购物车=
[]
?@DennisVash?尽管此代码片段可以回答这个问题,包括对为什么以及如何帮助解决问题的解释可以提高答案的质量和使用寿命。请参阅。尽管此代码片段可能会回答此问题,但包括对其帮助解决问题的原因和方式的解释会提高答案的质量和寿命。看见