英文:
update state from context return error 'setState is not a function'
问题
Description
我正在使用React.JS中的useContext中的上下文创建一个暗/亮模式功能,但我遇到了一个问题。我对在React.js中使用useContext还很新,所以我请求帮助找到问题。
Problem
当我设置主题时,它返回错误。
Code
ModeProvider.js
import React, { useState } from "react"
export const ModeContext = React.createContext([])
const ModeProvider = ({ children }) => {
const [darkMode, setDarkMode] = useState(true)
return (
<ModeContext.Provider value={[darkMode, setDarkMode]}>
{children}
</ModeContext.Provider>
)
}
export default ModeProvider
ModeButton.js (作为组件)
import React from "react"
import DayIcon from "images/daymode.svg"
import NightIcon from "images/nightmode.svg"
import { ImgButton } from "./ModeButton.styles"
const ModeButton = ({ darkMode, setDarkMode }) => (
<ImgButton
src={darkMode ? NightIcon : DayIcon}
alt="mode"
onClick={() => setDarkMode(prevState => !prevState)}
/>
)
export default ModeButton
Header.js
import React, { useState, useContext } from 'react'
import { Link } from 'gatsby'
import useConfigQuery from '../../hooks/useConfigQuery'
import { Wrapper, Logo } from './Header.styles'
import Menu from 'components/Menu'
import Hamburger from 'components/Hamburger'
import MobileMenu from 'components/MobileMenu'
import ModeButton from "components/ModeButton"
import { ModeContext } from '../../context/ModeProvider'
const Header = ({ siteTitle = "" }) => {
const [darkMode, setDarkMode] = useContext(ModeContext);
const [menuOpen, setMenuOpen] = useState(false);
const siteConfig = useConfigQuery()
return (
<Wrapper>
<Hamburger menuOpen={menuOpen} setMenuOpen={setMenuOpen}/>
<MobileMenu menuOpen={menuOpen} items={siteConfig.menu}/>
<Menu items={siteConfig.menu}/>
<Link to="/">
<Logo src={siteConfig.logo.publicURL} alt={siteTitle}/>
</Link>
<ModeButton darkMode={darkMode} setDarkMode={setDarkMode} />
</Wrapper>
)
}
export default Header;
Layout.js
import Header from 'components/Header'
import { ModeContext } from 'context/ModeProvider'
const Layout = ({ children }) => {
const [darkMode] = useContext(ModeContext);
const data = useMetaDataQuery();
console.log(data)
return (
<ThemeProvider theme={darkMode ? darkTheme : lightTheme}>
<GlobalStyles/>
<Header siteTitle={data.title}/>
{children}
</ThemeProvider>
)
}
Question
如何修复更新状态代码,以便解决错误? 任何帮助都将不胜感激,谢谢!
英文:
Description
I am creating a dark/light mode feature using context in useContext in React.JS, but I am encountering a problem. I am new to using useContext in react.js, so I am asking for help to find the problem.
Problem
When i set theme, it returns error.
Code
ModeProvider.js
import React, { useState } from "react"
export const ModeContext = React.createContext([])
const ModeProvider = ({ children }) => {
const [darkMode, setDarkMode] = useState(true)
return (
<ModeContext.Provider value={[darkMode, setDarkMode]}>
{children}
</ModeContext.Provider>
)
}
export default ModeProvider
ModeButton.js (as component)
import React from "react"
import DayIcon from "images/daymode.svg"
import NightIcon from "images/nightmode.svg"
import { ImgButton } from "./ModeButton.styles"
const ModeButton = ({ darkMode, setDarkMode }) => (
<ImgButton
src={darkMode ? NightIcon : DayIcon}
alt="mode"
onClick={() => setDarkMode(prevState => !prevState)}
/>
)
export default ModeButton
Header.js
import React, { useState, useContext } from 'react'
import { Link } from 'gatsby'
import useConfigQuery from '../../hooks/useConfigQuery'
import { Wrapper, Logo } from './Header.styles'
import Menu from 'components/Menu'
import Hamburger from 'components/Hamburger'
import MobileMenu from 'components/MobileMenu'
import ModeButton from "components/ModeButton"
import { ModeContext } from '../../context/ModeProvider'
const Header = ({ siteTitle = "" }) => {
const [darkMode, setDarkMode] = useContext(ModeContext);
const [menuOpen, setMenuOpen] = useState(false);
const siteConfig = useConfigQuery()
return (
<Wrapper>
<Hamburger menuOpen={menuOpen} setMenuOpen={setMenuOpen}/>
<MobileMenu menuOpen={menuOpen} items={siteConfig.menu}/>
<Menu items={siteConfig.menu}/>
<Link to="/">
<Logo src={siteConfig.logo.publicURL} alt={siteTitle}/>
</Link>
<ModeButton darkMode={darkMode} setDarkMode={setDarkMode} />
</Wrapper>
)
}
export default Header;
Layout.js
import Header from 'components/Header'
import { ModeContext } from 'context/ModeProvider'
const Layout = ({ children }) => {
const [darkMode] = useContext(ModeContext);
const data = useMetaDataQuery();
console.log(data)
return (
<ThemeProvider theme={darkMode ? darkTheme : lightTheme}>
<GlobalStyles/>
<Header siteTitle={data.title}/>
{children}
</ThemeProvider>
)
}
Question
How to fix the update state code so the error can be resolved ?
Any help will be appreciated, thank you
答案1
得分: 1
你没有提供ModeContext.Provider
,你没有将它包含在组件层次结构中(或者如果你包含了,你没有展示代码的这一部分)。
它需要在另一个组件中,这个组件_包含_了你调用useContext(ModeContext)
的所有组件,例如:
import Header from 'components/Header'
import ModeProvider, { ModeContext } from 'context/ModeProvider'
const Layout = ({ children }) => (
<ModeProvider>
<InnerLayout>{children}</InnerLayout>
</ModeProvider>
);
const InnerLayout = ({ children }) => {
const [darkMode] = useContext(ModeContext);
const data = useMetaDataQuery();
console.log(data)
return (
<ThemeProvider theme={darkMode ? darkTheme : lightTheme}>
<GlobalStyles/>
<Header siteTitle={data.title}/>
{children}
</ThemeProvider>
)
}
英文:
You don't appear to be providing a ModeContext.Provider
– you don't include it in the component hierarchy (or, if you do you're not showing that bit of the code).
It will need to be in another component that encloses all components where you're calling useContext(ModeContext)
, e.g.
import Header from 'components/Header'
import ModeProvider, { ModeContext } from 'context/ModeProvider'
const Layout = ({ children }) => (
<ModeProvider>
<InnerLayout>{children}</InnerLayout>
</ModeProvider>
);
const InnerLayout = ({ children }) => {
const [darkMode] = useContext(ModeContext);
const data = useMetaDataQuery();
console.log(data)
return (
<ThemeProvider theme={darkMode ? darkTheme : lightTheme}>
<GlobalStyles/>
<Header siteTitle={data.title}/>
{children}
</ThemeProvider>
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论