我尝试运行此代码,但它给了我一个运行时错误说
TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))
这是代码。
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;
错误指向这行代码const [{}, dispatch] = useContext(GlobalContext);
GlobalState 代码
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>
);
};