类型错误:无法解构属性“主题”;的(0,react__WEBPACK_IMPORTED_MODULE_0__.useContext)(...)'因为它是空的
我正在使用 TypeScript 开发 Next.js 应用程序,但出现此错误:
“TypeError: Cannot destruct property 'theme' of '(0,react__WEBPACK_IMPORTED_MODULE_0__.useContext)(...)',因为它为 null。”
这是我的 _app.tsx 文件:
import { useContext } from "react"
import { ThemeProvider } from "styled-components"
import ContextProvider from "../context/Context"
import { Context } from "../context/Context"
import GlobalStyle from "../styles/global"
export default function App({ Component, pageProps }) {
const { theme } = useContext(Context)
return (
<ContextProvider>
<ThemeProvider theme={theme}>
<GlobalStyle />
<Component {...pageProps} />
</ThemeProvider>
</ContextProvider>
)
}
这是我的 Context.tsx 文件:
import { createContext, useState } from "react"
import Light from "../styles/themes/Light"
import Dark from "../styles/themes/Dark"
interface Props {
theme: any;
setTheme: any;
handleTheme(): void;
}
export const Context = createContext<Props>(null)
export default function ContextProvider({ children }) {
const [theme, setTheme] = useState(Dark)
function handleTheme() {
setTheme(theme.title === "Dark" ? Light : Dark)
}
return(
<Context.Provider
value={{
theme,
setTheme,
handleTheme
}}
>
{children}
</Context.Provider>
)
}
我收到此错误,但不知道如何解决它。
有人可以帮助我吗?
I'm working on a Next.js application with TypeScript and I have this error:
"TypeError: Cannot destructure property 'theme' of '(0 , react__WEBPACK_IMPORTED_MODULE_0__.useContext)(...)' as it is null."
This is my _app.tsx file:
import { useContext } from "react"
import { ThemeProvider } from "styled-components"
import ContextProvider from "../context/Context"
import { Context } from "../context/Context"
import GlobalStyle from "../styles/global"
export default function App({ Component, pageProps }) {
const { theme } = useContext(Context)
return (
<ContextProvider>
<ThemeProvider theme={theme}>
<GlobalStyle />
<Component {...pageProps} />
</ThemeProvider>
</ContextProvider>
)
}
This is my Context.tsx file:
import { createContext, useState } from "react"
import Light from "../styles/themes/Light"
import Dark from "../styles/themes/Dark"
interface Props {
theme: any;
setTheme: any;
handleTheme(): void;
}
export const Context = createContext<Props>(null)
export default function ContextProvider({ children }) {
const [theme, setTheme] = useState(Dark)
function handleTheme() {
setTheme(theme.title === "Dark" ? Light : Dark)
}
return(
<Context.Provider
value={{
theme,
setTheme,
handleTheme
}}
>
{children}
</Context.Provider>
)
}
I've been getting this error and I'm not sure how to solve it.
Can someone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 ContextProvider 组件中使用“createContext”的行需要有一个基于接口“Props”的初始对象,并且该对象不为空。
The line where you use 'createContext' in your ContextProvider component needs to have an initial object based on your interface 'Props' and not null.
在 Nextjs 13 中,您需要将“use client”指令放在组件的顶部。
In Nextjs 13 you need to put the "use client" directive in the top of your component.
我遇到了类似的错误,地图不是主题:
我有这段代码并单击弹出窗口导致了错误:
将弹出窗口包装在地图中解决了它:
I was getting a similar error, with map not theme:
I had this code and clicking a popup caused the error:
Wrapping the Popup in the Map solved it: