简体   繁体   English

使用 React-Context 时对象不可迭代错误

[英]Object is not iterable Error while Using React-Context

So I recently started using React and am learning the Context Api.所以我最近开始使用 React 并且正在学习 Context Api。 The zest of my code is:我的代码的热情是:

export const UsersContext = createContext();
export function UsersProvider(props) {
    const fetchedItem = async function () {
        const data = await fetch('https://jsonplaceholder.typicode.com/users');
        const users = await data.json();
        setUsers(users);
    };
    const [users, setUsers] = useState([]);
    useEffect(function () {
        fetchedItem();
    }, []);

    return (
        <UsersContext.Provider value={[users, setUsers]}>{props.children}</UsersContext.Provider>
    );
}

And I try to import and iterate over the users array like so:我尝试像这样导入和迭代用户数组:

import { UsersContext, UsersProvider } from './UsersContext';
export default function UsersPage() {
    const [users, setUsers] = useContext(UsersContext);
return (
        <UsersProvider>
{users.map(function (user) {
//I do something with user object});}
</UsersProvider>

The issue is this does not work and I am not sure what the terminologies are to search it on the web.问题是这不起作用,我不确定在网上搜索它的术语是什么。 I get TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) which to me means that users is not an array?我得到TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))这对我来说意味着users不是数组? But it is as far as I am concerned.但就我而言,它是。 Any suggestions for fixing/debugging this?任何修复/调试此问题的建议?

You're using useContext outside of the provider, you should use useContext in a child component.您在提供程序之外使用useContext ,您应该在子组件中使用useContext

import { UsersContext, UsersProvider } from './UsersContext';
export default function UsersPage() {
  const [users, setUsers] = useContext(UsersContext);
  return (
    <UsersProvider>
      {users.map(function (user) {
        <div key={user.id}>{user.id}</div>
      })}
    </UsersProvider>
  );
}

is the same as是相同的

import { UsersContext, UsersProvider } from './UsersContext';
export default function UsersPage() {
  return (
    <UsersContext.Consumer>
      {([users, setUsers]) => (
        <UsersProvider>
          {users.map(function(user) {
            <div key={user.id}>{user.id}</div>;
          })}
        </UsersProvider>
      )}
    </UsersContext.Consumer>
  );
}

as you can see the consumer is not a child of the provider, thus there is nothing provided to it.如您所见,消费者不是提供者的孩子,因此没有提供任何内容。 Consuming it in a child component works.在子组件中使用它是有效的。

import { UsersContext, UsersProvider } from './UsersContext';
export default function UsersPage() {
  return (
    <UsersProvider>
      <UsersPageChild />
    </UsersProvider>
  );
}

function UsersPageChild() {
  const [users, setUsers] = useContext(UsersContext);
  return (
    <div>
      {users.map(function(user) {
        <div key={user.id}>{user.id}</div>;
      })}
    </div>
  );
}

暂无
暂无

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

相关问题 如何修复:React-Context - TypeError: Object 不可迭代(无法读取属性 Symbol(Symbol.iterator)) - How to fix: React-Context - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) 使用 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))" TypeError: dispatch is not a function when using react-context api - TypeError: dispatch is not a function when using react-context api 用 react-context 包装 App 组件后的空白屏幕 - blank screen after wrapping App component with react-context 未捕获的类型错误:object 不可迭代 --&gt; 使用减速器上下文 - Uncaught TypeError: object is not iterable -->using context with reducer React Context TypeError:BlurChangeValue 不可迭代 - React Context TypeError: BlurChangeValue is not iterable 在我的react-es6项目中使用“上下文”时出错 - Error while using “context” in my react-es6 project React - 使用 Hooks 时,出现错误 - Object 不可迭代(无法读取属性 Symbol (Symbol.iterator)) - React - When using Hooks, I get an error - Object is not iterable (cannot read property Symbol (Symbol.iterator)) 为什么当没有从提供者传递任何值时,react-context 返回 undefined,而不是返回默认值? - why react-context is returning undefined when no value has been passed from provider, rather than returning a default value? React context throwing TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) - React context throwing TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM