当使用带有笑话和酶的自定义钩子时,出现此错误'TypeError:无法读取未定义的属性'Symbol(Symbol.iterator)''
尝试了各种方法,但似乎无济于事。请帮助
import React, { useEffect, useState, Fragment } from 'react';
/* Utils */
import { useSystemContext } from '../../../utils/Context/SystemContextProvider';
/* Components */
import MenuStructure from './MenuStructure';
const Menu = () => {
const [{ siteData }] = useSystemContext();
const [open, setOpen] = React.useState(false);
const openMenu = () => {
setOpen(!open);
};
const componentProps = {
menuItems: siteData,
openMenu: openMenu,
open: open
};
return (
<MenuStructure {...componentProps} />
)
};
export default Menu;
测试
import { shallow, mount } from 'enzyme';
import Menu from '.';
import { testUseState } from '../../../utils/TestUtils';
describe('<Menu />', () => {
let wrapper;
it('calls setCount with 0', () => {
wrapper = mount(<Menu />);
});
});
错误
TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
9 | import MenuStructure from './MenuStructure';
10 |
> 11 | const Menu = () => {
| ^
12 | const [{ siteData }] = useSystemContext();
13
根据要求添加了useSystemContext
import React, { createContext, useContext, useReducer } from 'react';
export const SystemContext = createContext();
export const SystemContextProvider = ({ reducer, initialState, children }) => (
<SystemContext.Provider value={useReducer(reducer, initialState)}>
{children}
</SystemContext.Provider>
);
export const useSystemContext = () => useContext(SystemContext);
这在网站上运行时效果很好