【发布时间】:2021-02-13 21:57:45
【问题描述】:
我有两个钩子SideBarTneme和GradientThemes在第二个钩子里面有不同属性的对象,我需要得到两个属性(SideBarThemePage和SideBarThemeName)并把它放在SideBarTheme里面,因为我使用上下文我在另一个组件中获取这些值,但问题是我得到一个名为“Test”的值而不是两个值
如您所知,我需要为此获取两个值
SideBarThemePage: "Test", 和 SideBarThemeName: "AquaGradient",
我在其他组件中获取这些值以满足我的需要,这是我的代码
export default function ThemeDoc(props) {
const [SideBarTheme, SetSideBarTheme] = useState(localStorage.getItem("SideBarKey"));
// Gradient Themes
const [GradientThemes,] = useState(
[
{
SideBarThemePage: "Test",
SideBarThemeName: "AquaGradient",
RadioButtonStyle: {
RadioButtonColor: "linear-gradient(40deg, #51adff, #05ffa3)",
RadioButtonBorder: "1px solid #33b7de",
}
},
{
SideBarThemePage: "Test",
SideBarThemeName: "RedGradient",
RadioButtonStyle: {
RadioButtonColor: "linear-gradient(120deg,#ffb2b2 0,#f68084 100%)",
RadioButtonBorder: "1px solid #ff8185fa",
},
},
]
)
useEffect(() => {
localStorage.setItem("SideBarKey", SideBarTheme);
})
const SideBarPageContent = (SideBarThemePage, SideBarThemeName) => {
localStorage.setItem(SideBarThemePage, 'SideBarKey');
localStorage.setItem(SideBarThemeName, 'SideBarKey');
SetSideBarTheme(SideBarThemePage);
SetSideBarTheme(SideBarThemeName);
}
const gradientList = GradientThemes.map((gradTheme, index) => {
return (
<RadioButton
key={index}
style={{background: gradTheme.RadioButtonStyle.RadioButtonColor, border: gradTheme.RadioButtonStyle.RadioButtonBorder}}
className={"RadioButton_ThemeDoc"}
name="group1"
value="first"
onChange={() => SideBarPageContent(gradTheme.SideBarThemeName, gradTheme.SideBarThemePage)}
/>
);
})
return (
<div className="page-wrapper chiller-theme toggled">
<CounterContext.Provider value={{
SideBarValue: [SideBarTheme, SetSideBarTheme],
}}>
<SideBar gradientList={gradientList} {...props} />
<PageContent {...props} />
</CounterContext.Provider>
</div>
);
}
【问题讨论】:
标签: javascript arrays reactjs object react-hooks