Francisco Pena
Francisco Pena

Reputation: 73

TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable,

I am trying to use the API context on react native and I am getting the following error below.

TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a Symbol.iterator method.

This is what my code looks like

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,
  };
};

Then I am using it like so

const {purchases, setPurchases} = usePurchasesContext();

Does anybody know why this is happening and how can I fix it been trying for hours now?

Upvotes: 3

Views: 3713

Answers (1)

Francisco Pena
Francisco Pena

Reputation: 73

Nevermind I solved it by doing the following

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

Upvotes: 2

Related Questions