简体   繁体   English

将项目添加到 Reducer 中的空数组中会给出错误“类型错误:传播不可迭代实例的尝试无效”。

[英]Add Items into Empty array in Reducer give Error 'TypeError: Invalid attempt to spread non-iterable instance.'

I'm tried to declare any empty array in reducer and trying to add some objects in it dynamically.But I got this error when i try to add new item in it.'TypeError: Invalid attempt to spread non-iterable instance.'It means that you don't have any array in which you are trying to add new item?.How's it possible.Here is my reducer state.我试图在 reducer 中声明任何空数组并尝试在其中动态添加一些对象。但是当我尝试在其中添加新项目时出现此错误。'TypeError: Invalid trial to spread non-iterable instance.'It意味着您没有任何要在其中添加新项目的数组?怎么可能。这是我的减速器状态。

const initialState = {
  
  recentSearches: [],
 
};

here is my function firing up这是我的功能启动

const handleItem = async (item) => {
    let recent = [...recentSearches];
    recent.push(item);
    new Promise((rsl, rej) => {
      addSearchItem(recent, rsl, rej);
    })
      .then((res) => {
        navigation.navigate('SearchDetail', {text: item.name});

        // console.log('Zaid ----->', res);
      })
      .catch((res) => {
        console.log(res);
      });
  };

here is the action to reducer这是减速器的动作

export const addSearchItem = (data, rsl, rej) => {
  return (dispatch) => {
    try {
      dispatch({
        type: ADD_SEARCH,
        recentSearches: data,
      });
      rsl();
    } catch (err) {
      rej();
    }
  };
};

here is the case这是这种情况

case ADD_SEARCH:
      return {
        ...state,
        recentSearches: action.recentSearches,
      };

you can update an array in redux immutably in this way您可以通过这种方式不可变地更新 redux 中的数组

return {
...state,
recentSearches:state.recentSearches.concat(item),
}

I hope this will help for more follow this https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns我希望这将有助于更多关注这个https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns

This error happens when you trying to copy an undefined array in your reducer.当您尝试在减速器中复制未定义的数组时会发生此错误。 eg:例如:

const newList = [...state.someList];

In this case, 'someList' is undefined or not an array.在这种情况下,'someList' 未定义或不是数组。 make sure your spelling or you are using the correct array.确保您的拼写或您使用的是正确的数组。

尚未修复 上面提供的解决方案

暂无
暂无

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

相关问题 React Native 出现错误“传播不可迭代实例的尝试无效。” 在将数据添加到空数组时 - React Native getting error “Invalid attempt to spread non-iterable instance.” while adding data to empty array Usestate + Setstate [TypeError: Invalid attempt to spread non-iterable instance.] - React Native Form Handling - Usestate + Setstate [TypeError: Invalid attempt to spread non-iterable instance.] - React Native Form Handling 未处理的拒绝(TypeError):散布不可迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance React Uncaught TypeError:传播不可迭代实例的无效尝试 - React Uncaught TypeError: Invalid attempt to spread non-iterable instance 类型错误:传播不可迭代实例和合成事件的尝试无效 - TypeError: Invalid attempt to spread non-iterable instance and Synthetic Events TypeError:传播不可迭代实例的无效尝试 - TypeError: Invalid attempt to spread non-iterable instance 获取错误为“传播不可迭代实例的尝试无效。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法' - Getting error as 'Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method' TypeError:无效的解构不可迭代实例的尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() - TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() 传播不可迭代实例的尝试无效 - Invalid attempt to spread non-iterable instance “TypeError:传播不可迭代实例的无效尝试”:将数组或 Object 添加到数组 - “TypeError: Invalid attempt to spread non-iterable instance”: Adding either an Array or Object to an Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM