[英]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.