admin管理员组文章数量:1298180
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>
);
};
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>
);
};
Share
Improve this question
edited Jul 17, 2020 at 3:49
Dilhan Nakandala
3636 silver badges26 bronze badges
asked Jul 17, 2020 at 2:49
PaschalPaschal
1432 gold badges3 silver badges9 bronze badges
2
- You need to implement an iterator. Or you can use Object.keys, Object.values, or Object.entries – Dom Commented Jul 17, 2020 at 3:52
- 1 I tried what you posted in a codeSandbox and there's no error. Maybe the problem lies in some code you haven't posted. https://codesandbox.io/s/infallible-hugle-6m4iv?file=/src/globalContext.js – EvanMorrison Commented Jul 17, 2020 at 3:57
3 Answers
Reset to default 3I 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 ponent.
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.
It looks like you forgot to wrap your ponent with provider you created as you currently use it:
<GlobalState>
<MediaCard />
</GlobalState>
Below is my code, it is just an example to show you why the error is occurring in your code.
The data layer you created is not been wrapped around the ponents of the whole web app therefore it's not able to be recognized by the MediaCard
ponent... what you need to do is just go in your index.js
and wrap the StateProvider
around the main app
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import reducer, { initialState } from "./reducer";
import { StateProvider} from'./StateProvider';
ReactDOM.render(
<React.StrictMode>
<StateProvider initialState={ initialState } reducer={ reducer }>
<App />
</StateProvider>
</React.StrictMode>,
document.getElementById('root')
);
版权声明:本文标题:javascript - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741315016a2371864.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论