gpt4 book ai didi

reactjs - TypeError : Invalid attempt to destructure non-iterable instance. 为了迭代,

转载 作者:行者123 更新时间:2023-12-04 15:12:21 24 4
gpt4 key购买 nike

我正在尝试在 React Native 上使用 API 上下文,但出现以下错误。

TypeError:解构不可迭代实例的无效尝试。为了可迭代,非数组对象必须具有 Symbol.iterator 方法。

我的代码是这样的

import React, {createContext, useState, useContext} from 'react';

const PurchasesContext = createContext();

export const PurchasesContextProvider = ({children}) => {
const [purchases, setPurchases] = useState([]);
return (
<PurchasesContext.Provider value={[purchases, setPurchases]}>
{children}
</PurchasesContext.Provider>
);
};

export const usePurchasesContext = () => {
const [purchases, setPurchases] = useContext(PurchasesContext);

return {
purchases,
setPurchases,
};
};

然后我就这样用了

const {purchases, setPurchases} = usePurchasesContext();

有人知道为什么会发生这种情况吗?我现在已经尝试了几个小时,该如何解决?

最佳答案

没关系,我通过执行以下操作解决了它

const PurchasesContext = createContext([[], () => {}]);

关于reactjs - TypeError : Invalid attempt to destructure non-iterable instance. 为了迭代,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64979948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com