简体   繁体   English

未捕获的类型错误:object 不可迭代 --> 使用减速器上下文

[英]Uncaught TypeError: object is not iterable -->using context with reducer

I am learning to use useContext and useReducer together but am getting an error I haven't managed to figure out:我正在学习一起使用 useContext 和 useReducer 但我遇到了一个我没有弄清楚的错误:

Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))未捕获的类型错误:object 不可迭代(无法读取属性 Symbol(Symbol.iterator))

It occurred after destructuring the context inside MealItemForm.js.它发生在解构 MealItemForm.js 中的上下文之后。 I tried setting default value in createContext, importing with and without curly braces etc. I am trying to dispatch the function inside children component.我尝试在 createContext 中设置默认值,导入带和不带大括号等。我试图在子组件中调度 function。 Any idea?任何想法?

MealItemForm.js MealItemForm.js

import React,{useContext} from 'react';
import MeatContext from '../store/meat-context';
import Input from './Input';
import styles from "./MealItemForm.module.css"

function MealItemForm(props) {
  
  const [meatState,dispatchMeatState]=useContext(MeatContext)
  const handleClick=(event)=>{
event.preventDefault();

  }  
  
  return (
      <form className={styles.form}>
          <Input/>
          <button onClick={handleClick}>+ Add</button>
      </form>
  );
}

export default MealItemForm;

meat-context.js肉上下文.js

import React,{useReducer,useState} from 'react';


const MeatContext = React.createContext({meatState:[]});

export function MeatContextProvider(props) {
  const meatReducer = (state, action) => {
    if (action.type === 'ADD_MEAT') {
      return [...meatState];
    }

    return {  };
  };
  
  const [meatState, dispatchMeatState] = useReducer(meatReducer,
    [

    ]
  );
  return (
<MeatContext.Provider value={{meatState:meatState,dispatchMeatState:dispatchMeatState}}>
  {props.children}
</MeatContext.Provider>
  );

  
}
export default MeatContext

The value provided to Context is an object提供给Context的值是 object

{meatState:meatState,dispatchMeatState:dispatchMeatState}

But you're using array destructuring while getting the values但是您在获取值时使用数组解构

const [meatState, dispatchMeatState ] = useContext(MeatContext)

Using object destructuring should solve it,使用 object 解构应该可以解决它,

const { meatState, dispatchMeatState } = useContext(MeatContext)

Restructuring with {object} instead of [array] solves the issue使用 {object} 而不是 [array] 进行重组可以解决问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “未捕获的 TypeError:function 不可迭代”在 [] 中使用 ...,但在 {} 中它可以工作...? - “Uncaught TypeError: function is not iterable” using … in [], but in {} it works…? 未捕获的TypeError:项目不可迭代 - Uncaught TypeError: items is not iterable 未捕获的类型错误:变量不可迭代 - Uncaught TypeError: variable is not iterable 未捕获的类型错误:将 useReducer 和 useContext 用于全局存储 React js 时,对象不可迭代 - Uncaught TypeError: Object is not iterable when using useReducer with useContext for global store React js 使用 React Context API 时出错:“TypeError:Object 不可迭代(无法读取属性 Symbol(Symbol.iterator))” - Error while using React Context API: "TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))" 类型错误:Object 不可迭代 - 在 Redux 中使用 useSelector 时 - TypeError: Object is not iterable - when using useSelector in Redux 在Phonegap上使用afui和jquery时,出现“对象函数(选择器,上下文)的未捕获的TypeError:属性&#39;touchLayer&#39;” - “Uncaught TypeError: Property 'touchLayer' of object function ( selector, context )” when using afui and jquery on phonegap React Context TypeError:BlurChangeValue 不可迭代 - React Context TypeError: BlurChangeValue is not iterable TypeError:“未定义”对象是否不可迭代? - TypeError: 'Undefined' object is not iterable? Uncaught (in promise): TypeError: data.data is not iterable - Uncaught (in promise): TypeError: data.data is not iterable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM