类型错误:无法解构属性“主题”;的(0,react__WEBPACK_IMPORTED_MODULE_0__.useContext)(...)'因为它是空的

发布于 2025-01-10 04:09:27 字数 1472 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗藏城府 2025-01-17 04:09:27

在 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.

悲歌长辞 2025-01-17 04:09:27

在 Nextjs 13 中,您需要将“use client”指令放在组件的顶部。

In Nextjs 13 you need to put the "use client" directive in the top of your component.

对风讲故事 2025-01-17 04:09:27

我遇到了类似的错误,地图不是主题:

类型错误:无法解构“(0,react_”的属性“map”

我有这段代码并单击弹出窗口导致了错误:

  <Map
    initialViewState={INITIAL_VIEW_STATE}
    style={{ width: '100%', height: '100%' }}
    mapStyle={mapStyle}
    mapboxAccessToken={accessToken}
    onMouseMove={handleMouseMove}>
    <DeckGLOverlay ref={deckRef} />
    <NavigationControl position="top-left" />
  </Map>
  {selected && (
      <Popup
        longitude={selected.LongitudeDeg}
        latitude={selected.LatitudeDeg}
        closeButton={true}
        closeOnClick={false}
        onClose={() => setSelected(null)}
        anchor="bottom"
      >
        <div>
          <p>Operation Number: {selected.OperationNumber}</p>
          <p>Altitude: {selected.AltitudeFtAMSL} ft AMSL</p>
          <p>Speed: {selected.Speed} knots</p>
        </div>
      </Popup>
    )}

将弹出窗口包装在地图中解决了它:

  <Map
    initialViewState={INITIAL_VIEW_STATE}
    style={{ width: '100%', height: '100%' }}
    mapStyle={mapStyle}
    mapboxAccessToken={accessToken}
    onMouseMove={handleMouseMove}>
    <DeckGLOverlay ref={deckRef} />
    <NavigationControl position="top-left" />

  {selected && (
      <Popup
        longitude={selected.LongitudeDeg}
        latitude={selected.LatitudeDeg}
        closeButton={true}
        closeOnClick={false}
        onClose={() => setSelected(null)}
        anchor="bottom"
      >
        <div>
          <p>Operation Number: {selected.OperationNumber}</p>
          <p>Altitude: {selected.AltitudeFtAMSL} ft AMSL</p>
          <p>Speed: {selected.Speed} knots</p>
        </div>
      </Popup>
    )}
  </Map>

I was getting a similar error, with map not theme:

TypeError: Cannot destructure property 'map' of '(0 , react_

I had this code and clicking a popup caused the error:

  <Map
    initialViewState={INITIAL_VIEW_STATE}
    style={{ width: '100%', height: '100%' }}
    mapStyle={mapStyle}
    mapboxAccessToken={accessToken}
    onMouseMove={handleMouseMove}>
    <DeckGLOverlay ref={deckRef} />
    <NavigationControl position="top-left" />
  </Map>
  {selected && (
      <Popup
        longitude={selected.LongitudeDeg}
        latitude={selected.LatitudeDeg}
        closeButton={true}
        closeOnClick={false}
        onClose={() => setSelected(null)}
        anchor="bottom"
      >
        <div>
          <p>Operation Number: {selected.OperationNumber}</p>
          <p>Altitude: {selected.AltitudeFtAMSL} ft AMSL</p>
          <p>Speed: {selected.Speed} knots</p>
        </div>
      </Popup>
    )}

Wrapping the Popup in the Map solved it:

  <Map
    initialViewState={INITIAL_VIEW_STATE}
    style={{ width: '100%', height: '100%' }}
    mapStyle={mapStyle}
    mapboxAccessToken={accessToken}
    onMouseMove={handleMouseMove}>
    <DeckGLOverlay ref={deckRef} />
    <NavigationControl position="top-left" />

  {selected && (
      <Popup
        longitude={selected.LongitudeDeg}
        latitude={selected.LatitudeDeg}
        closeButton={true}
        closeOnClick={false}
        onClose={() => setSelected(null)}
        anchor="bottom"
      >
        <div>
          <p>Operation Number: {selected.OperationNumber}</p>
          <p>Altitude: {selected.AltitudeFtAMSL} ft AMSL</p>
          <p>Speed: {selected.Speed} knots</p>
        </div>
      </Popup>
    )}
  </Map>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文