React - 使用 useContext 和 useReducer 的问题

时间:2021-01-29 15:57:17

标签: reactjs use-context use-reducer

嘿,我在使用 useContext(React Hooks)和 useReducer(用于存储具有 4 个字符串值的数组)时遇到问题。在下面的图片中解释了究竟是什么错误以及我做了什么。谢谢你

enter image description here App.js - 使用 ItemsByCategory(第 7 行)。使用 useContext 传递具有 2 个值的 Categories 组件对象 - CategoriesArray 和 CategoriesDisaptch(第 37-39 行)enter image description here 在类别组件中,添加 onClick 函数行时出现问题。没有它,工作正常。 为我显示的错误,添加为图像。 enter image description here

2 个答案:

答案 0 :(得分:0)

问题是您在 CategoriesArray 中将 categories 指定为 <ItemsByCategory.Provider/>。所以你必须改为CategoriesContext.categories.map(filterCategory)

答案 1 :(得分:0)

找到我的问题。在减速器功能不能做 - 返回; 我不想更新 categoryArray - 只想像我一样在 setMenuItems 函数中根据它们的类别显示特定图像 - 所以在两种情况下都返回状态。添加为 func 的参数。