简体   繁体   English

如何修复:React-Context - TypeError: Object 不可迭代(无法读取属性 Symbol(Symbol.iterator))

[英]How to fix: React-Context - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))

I have tried some solutions that came by, on this link particularily...我已经尝试了一些解决方案,特别是在这个链接上......

I tried changing value inside my TodosContext.js file.. which didn't work, too.. One more thing that I have tried is to call useContext() function from another component, that didn't work neither..我尝试在我的 TodosContext.js 文件中更改值.. 这也不起作用.. 我尝试的另一件事是从另一个组件调用 useContext() function,这也不起作用..

Here's my code.这是我的代码。 App.js:应用程序.js:

import React, { useState, useContext } from 'react';
import TodoList from './components/TodoList';
import NewTodo from './components/NewTodo';
import { TodosProvider, TodosContext } from './components/contextapi/TodosContext';

function App() {

  const [input, setInput] = useState('');
  const [todos, setTodos] = useContext(TodosContext);

  const _handleInput = (e) => {
    setInput(e.target.value)
  }

  const _todoAdd = (e) => {
    if (e.key === 'Enter') {
      setTodos(
        [...todos, { content: input, id: Date.now(), completed: false }]
      )
      setInput('')
    }
  }

  const _todoRemove = (id) => {
    const newTodos = todos.filter(todo => todo.id !== id)
    setTodos(newTodos)
  }

  return (
    <div>
      <header>
        <h3>To-Do Manager | Context API</h3>
      </header>
      <TodosProvider>
        <NewTodo newTodo={_todoAdd} handleInput={_handleInput} newVal={input} />
        <TodoList todos={todos} />
      </TodosProvider>
    </div>
  );
}

export default App;

TodosContext.js: TodosContext.js:

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

export const TodosContext = createContext()

export const TodosProvider = ({ children }) => {
    const [todos, setTodos] = useState([]);
    return (
        <TodosContext.Provider value={[todos, setTodos]}>{children}</TodosContext.Provider>
    )
}

TodoList.js: TodoList.js:

import React, { useContext } from 'react';
import Todo from './Todo';
import RemoveTodoFromList from './RemoveTodoFromList';
import { TodosContext } from './contextapi/TodosContext'

function TodoList() {

    const [todos, setTodos] = useContext(TodosContext);

    return (
        <div>
            {todos.map(todo => (
                <div>
                    <Todo key={todo.id} todo={todo} />
                </div>
            ))}
        </div>
    )
}

export default TodoList

I'm really struggling with this one, I spent whole day figuring out what went wrong.. Thanks!我真的在为此苦苦挣扎,我花了一整天时间弄清楚出了什么问题..谢谢!

We fixed it inside the comments.我们在评论中修复了它。

createContext needs an object as parameter that defines your context. createContext需要一个 object 作为定义上下文的参数。 In your case it should be export const TodosContext = createContext([[],() => {}]) .在您的情况下,它应该是export const TodosContext = createContext([[],() => {}]) So the application knows the first element of the tuple is an array and so iterable.所以应用程序知道元组的第一个元素是一个数组,因此是可迭代的。

暂无
暂无

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

相关问题 使用 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))" 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)) TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) × TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - × TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) 反应js:TypeError:对象null不可迭代(无法读取属性Symbol(Symbol.iterator)) - react js : TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator)) TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - 为什么? 这是什么意思? 如何解决? - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - Why? What does it mean? How to solve it? TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) JavaScript - TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) JavaScript 未捕获的类型错误:对象在 XMLHttpRequest 中不可迭代(无法读取属性 Symbol(Symbol.iterator))。<anonymous> - Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) at XMLHttpRequest.<anonymous> React - 使用 Hooks 时,出现错误 - Object 不可迭代(无法读取属性 Symbol (Symbol.iterator)) - React - When using Hooks, I get an error - Object is not iterable (cannot read property Symbol (Symbol.iterator)) 获得未定义是不可迭代的(无法读取属性 Symbol(Symbol.iterator)) - getting undefined is not iterable (cannot read property Symbol(Symbol.iterator))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM