简体   繁体   English

React Context TypeError:BlurChangeValue 不可迭代

[英]React Context TypeError: BlurChangeValue is not iterable

I am using in this example React Context and local storage using React Context I want to get value in another component but get BlurChangeValue is not iterable:我在此示例中使用React Context和使用 React Context 的本地存储我想在另一个组件中获取值,但获取BlurChangeValue是不可迭代的:

在此处输入图像描述

SideBarBlurChange.jsx SideBarBlurChange.jsx

import React, {useState, createContext} from "react";
import {getTrackBackground, Range} from "react-range";

const STEP = 0.1;
const MIN = 0;
const MAX = 100;

export const BlurContext = createContext([]);

export default function SideBarBlurChange(props) {

    const ls = parseInt(window.localStorage.getItem('values'));
    const [values, SetValues] = useState(ls ? [ls] : [50]);

    const SaveChanges = () => {
        localStorage.setItem('values', values);
    }

    return (
        <>
            <BlurContext.Provider value={{
                BlurChangeValue: [values, SetValues],
            }
            }>
                <div
                    style={{
                        display: "flex",
                        justifyContent: "center",
                        flexWrap: "wrap",
                    }}
                    // More jsx
           </BlurContext.Provider>
        </>
    );
}

SideBar.jsx侧边栏.jsx

export default function SideBar(props) {

    const {BlurChangeValue} = React.useContext(BlurContext)
    const [values] = BlurChangeValue;

    return (
        <div className={"headline_sidebar_wrapper"}>
            <article className={`${Blur.glass} ${Blur.up}`}>
                <nav id="sidebar" className="sidebar-wrapper modal">
                    <div style={{backgroundImage: `url(${SideBarBackground})`}}>
                        <div style={{background: SideBarTheme, backdropFilter: `blur(${values})`}}
                             className={`${Blur.SideBar_Page_Content} ${Blur.SideBarContainer}`}>
                        </div>
                    </div>
                </nav>
            </article>
        </div>
    );
}

I want to use the value values inside JSX to get a blur effect, all other contexts work fine, I just deleted them to make it easier to read the code.我想使用 JSX 中的values来获得模糊效果,所有其他上下文都可以正常工作,我只是删除了它们以便更容易阅读代码。

I think the main issue here is the initial value of BlurContext .我认为这里的主要问题是BlurContext的初始值。 Try as instead:改为尝试:

export const BlurContext = createContext({
   BlurChangeValue: [],
});

Read further about React.createContext .进一步阅读React.createContext

you have to destruct the context object in the SideBar element first:您必须首先破坏SideBar元素中的上下文 object :

const {BlurChangeValue} = {...React.useContext(BlurContext)}

暂无
暂无

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

相关问题 类型错误:状态在 react 和 redux 上不可迭代 - TypeError: state is not iterable on react and redux React,获取 TypeError:在将全局状态与 Context 结合使用时,尝试解构不可迭代的实例无效 - React, Getting TypeError: Invalid attempt to destructure non-iterable instance when using global state with Context 如何修复: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))" 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)) 未捕获的类型错误:object 不可迭代 --&gt; 使用减速器上下文 - Uncaught TypeError: object is not iterable -->using context with reducer 无法使 Context API 在 React 应用程序上工作,未捕获的 TypeError: react__WEBPACK_IMPORTED_MODULE_0__.useContext(...) is not iterable - Can't make Context API work on React application, Uncaught TypeError: react__WEBPACK_IMPORTED_MODULE_0__.useContext(...) is not iterable React Context:TypeError:render不是函数 - React Context: TypeError: render is not a function React.js:未捕获的 TypeError:response.data.posts 不可迭代 - React.js: Uncaught TypeError: response.data.posts is not iterable React Uncaught TypeError:传播不可迭代实例的无效尝试 - React Uncaught TypeError: Invalid attempt to spread non-iterable instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM