Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))

I tried running this code, but it's giving me a runtime error saying

TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))

here is the code.

import React, { useContext} from "react";
import { GlobalContext } from '../GlobalState';

const MediaCard = ({ songs, categotyTitle }) => {
  const [{}, dispatch] = useContext(GlobalContext);
  const setCurrentVideoSnippet = data => {
    dispatch({ type: "setCurrentVideoSnippet", snippet: data });
  };
export default MediaCard;

the error is pointing at this line of code const [{}, dispatch] = useContext(GlobalContext);

the GlobalState code

import React, { useReducer } from "react";

export const GlobalContext = React.createContext();

const initialState = {
  currentVideoSnippet: {}, 
};

const reducer = (state, action) => {
  switch (action.type) {
    case "setCurrentVideoSnippet":
      return {
        ...state,
        currentVideoSnippet: action.snippet
      };
 default:
      return state;
  }
};

export const GlobalState = props => {
  const globalState = useReducer(reducer, initialState);
  return (
    <GlobalContext.Provider value={globalState}>
      {props.children}
    </GlobalContext.Provider>
  );
};
like image 945
Paschal Avatar asked Apr 06 '26 17:04

Paschal


2 Answers

I know this doesn't solve the OP's issue, but for those like me who wound up on this page with the same run time error message, my issue was caused because of the way I was importing the GlobalContext component.

Wrong Way: import GlobalContext from '../GlobalState';

Correct Way: import { GlobalContext } from '../GlobalState';

This fixed the issue I was running into with the same error message.

like image 116
Cody Thompson Avatar answered Apr 09 '26 07:04

Cody Thompson


It looks like you forgot to wrap your component with provider you created as you currently use it:

<GlobalState>
  <MediaCard />
</GlobalState>
like image 27
tmhao2005 Avatar answered Apr 09 '26 05:04

tmhao2005



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!