简体   繁体   English

React - 使用 Hooks 时,出现错误 - Object 不可迭代(无法读取属性 Symbol (Symbol.iterator))

[英]React - When using Hooks, I get an error - Object is not iterable (cannot read property Symbol (Symbol.iterator))

I use hooks inside the LessonThemes component, using the context, I try to access the color value inside the Test component, but I get an error我在LessonThemes组件中使用钩子,使用上下文,我尝试访问Test组件中的color值,但出现错误

Object is not iterable (cannot read property Symbol (Symbol.iterator)) Object 不可迭代(无法读取属性符号(Symbol.iterator))

LessonThemes.jsx课程主题.jsx

import React, {useState, useEffect, createContext} from "react";
import ThemeContext from "./ThemeContext";

export const CounterContext = createContext();

export default function LessonThemes(props) {
    const [color, setColor] = useState(localStorage.getItem("color"));

    const [themes, setThemes] = useState([
        { name: "G", color: "green" },
        { name: "R", color: "red" },
        { name: "B", color: "blue" },
    ])

    useEffect(() => {
        localStorage.setItem("color", color);
    })

    const SideBarPageContent = (SideBarPageContentBackground) => {
        localStorage.setItem('color', SideBarPageContentBackground);
        setColor(SideBarPageContentBackground);
    }

    return (
        <CounterContext.Provider value={[color, setColor]}>
            {
                themes.map((theme, index) => {
                    return (
                        <label key={index}>
                            <input
                                onChange={() => SideBarPageContent(theme.color)}
                                type="radio"
                                name="background"
                            />{theme.name}</label>
                    );
                })
            }
        </CounterContext.Provider>
    );
}

Test.jsx测试.jsx

export default function Test(props) {

  const [color] = useContext(LessonThemes);

  return (
    <div>
      <div className="sidebar-brand-container">
        <LessonThemes />
      </div>
      <div>
        <span style={{ background: color }} href="#">Theme</span>
      </div>
    </div>
  );
}

LessonThemes is a react component, it's providing the context to its children. LessonThemes是一个反应组件,它为它的孩子提供上下文。 CounterContext is the context you need to access in Test . CounterContext是您需要在Test中访问的上下文。

import { CounterContext } from '../path/to/CounterContext';

export default function Test(props) {

  const [color] = useContext(CounterContext);

  return (
    <div>
      <div className="sidebar-brand-container">
        <LessonThemes />
      </div>
      <div>
        <span style={{ background: color }} href="#">Theme</span>
      </div>
    </div>
  );
}

You should probably also define an initial context value in case Test isn't being rendered into a React DOMTree with a CounterContext as an ancestor.您可能还应该定义一个初始上下文值,以防Test没有被渲染到一个以CounterContext作为祖先的 React DOMTree 中。

export const CounterContext = createContext([]);

暂无
暂无

声明:本站的技术帖子网页,遵循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))" × 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)) 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)) 如何修复:React-Context - TypeError: Object 不可迭代(无法读取属性 Symbol(Symbol.iterator)) - How to fix: React-Context - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) 获得未定义是不可迭代的(无法读取属性 Symbol(Symbol.iterator)) - getting undefined is not iterable (cannot read property Symbol(Symbol.iterator)) 单击 React NavLink 会导致错误:Uncaught (in promise) TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) - Clicking on React NavLink causes error: Uncaught (in promise) TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) Firebase 函数问题:(未定义)对象不可迭代(无法读取属性 Symbol(Symbol.iterator)) - Firebase Functions trouble: (undefined) object is not iterable (cannot read property Symbol(Symbol.iterator)) 未捕获的类型错误:对象在 XMLHttpRequest 中不可迭代(无法读取属性 Symbol(Symbol.iterator))。<anonymous> - Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) at XMLHttpRequest.<anonymous>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM