简体   繁体   English

如何修复“TypeError:无法读取未定义的属性(读取'useSystemColorMode')”

[英]How to fix "TypeError: Cannot read properties of undefined (reading 'useSystemColorMode')"

So I am using ChakraUI and React JS to make a project.所以我正在使用 ChakraUI 和 React JS 来制作一个项目。 Whenever I start the project it gives me the error, "TypeError: Cannot read properties of undefined (reading 'useSystemColorMode')".每当我启动项目时,它都会给我错误“TypeError:无法读取未定义的属性(读取'useSystemColorMode')”。
I didn't edit any global theme or anything in chakra.我没有编辑任何全局主题或脉轮中的任何内容。 I just started making the navbar, designed with CSS.我刚开始制作导航栏,使用 CSS 设计。 And when I try to run it, it shows me the error.当我尝试运行它时,它会显示错误。 here is my navbar component这是我的导航栏组件

import React from "react";
import { MenuItems } from "./MenuItems";
import "./navbar.css";

function Navbar() {
  return (
    <>
      <nav className='NavbarItems'>
        <h1 className='navbar-logo'></h1>
        <div className='menu-icon'></div>
        <ul>
          {MenuItems.map((item, index) => {
            return (
              <li key={index}>
                <a className={item.cname} href={item.url}>
                  {item.title}
                </a>
              </li>
            );
          })}
        </ul>
      </nav>
    </>
  );
}

export default Navbar;

Navbar CSS file导航栏 CSS 文件

.NavbarItems {
  height: 80px;
  display: flex;
  justify-content: center;
}

App.js file应用程序.js 文件

import { ChakraProvider } from "@chakra-ui/provider";
import Navbar from "./components/navbar";
function App() {
  return (
    <ChakraProvider>
      <Navbar />
    </ChakraProvider>
  );
}

export default App;

For example, you're trying to call your useSystemColorMode method like this例如,您尝试像这样调用您的useSystemColorMode方法

this.arr.useSystemColorMode();

So "TypeError: Cannot read properties of undefined (reading 'useSystemColorMode')" means that this.arr is undefined.所以"TypeError: Cannot read properties of undefined (reading 'useSystemColorMode')"意味着this.arr是未定义的。 Debug your code to find out why调试您的代码以找出原因

So basically this is a problem with ChakraUI theme,even though I did not do anything with ChakraUI theme or whatsoever.所以基本上这是 ChakraUI 主题的问题,即使我没有对 ChakraUI 主题或任何东西做任何事情。 I added theme.js file which included the following:我添加了 theme.js 文件,其中包括以下内容:

// theme.js

// 1. import `extendTheme` function
import { extendTheme } from "@chakra-ui/react";

// 2. Add your color mode config
const config = {
  initialColorMode: "light",
  useSystemColorMode: true,
};

// 3. extend the theme
const theme = extendTheme({ config });

export default theme;

then I included the following in index.js file:然后我在 index.js 文件中包含以下内容:

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { ColorModeScript } from "@chakra-ui/color-mode";
import theme from "./theme";

ReactDOM.render(
  <React.StrictMode>
    <ColorModeScript initialColorMode={theme.config.initialColorMode} />
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);

And it worked.它奏效了。 If anyone facing the same problem, you can check out https://chakra-ui.com/docs/features/color-mode .如果有人遇到同样的问题,您可以查看https://chakra-ui.com/docs/features/color-mode

Once I used extendTheme instead of an object for theme the error was gone.一旦我使用 extendTheme 而不是 object 作为主题,错误就消失了。 https://chakra-ui.com/docs/theming/customize-theme https://chakra-ui.com/docs/theming/customize-theme

The first thing you should do is to check the name of the message type (error, success, warning) if you misspell it, the error will occure您应该做的第一件事是检查消息类型的名称(错误、成功、警告),如果拼写错误,则会发生错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 typeError: cannot read properties of undefined (reading 'upload') in vue - how to fix typeError: cannot read properties of undefined (reading 'upload') in vue 如何修复 Uncaught TypeError: Cannot read properties of undefined (reading 'toLocaleString') - how to fix the Uncaught TypeError: Cannot read properties of undefined (reading 'toLocaleString') 如何修复 TypeError:无法读取未定义的属性(读取“单词”)? - How to fix TypeError: Cannot read properties of undefined (reading 'words')? 如何修复 TypeError: Cannot read properties of undefined (reading 'split') in mongoose? - How can I fix TypeError: Cannot read properties of undefined (reading 'split') in mongoose? 如何修复此错误:“类型错误:无法读取 Discordjs 中未定义的属性(正在读取‘toLowerCase’)”? - How do I fix this error: "TypeError: Cannot read properties of undefined (reading 'toLowerCase')" in Discordjs? 如何修复此错误:未捕获(承诺中)类型错误:无法读取未定义的属性(读取“长度”) - how fix this error:Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length') 类型错误:无法读取未定义的属性(读取“和”) - TypeError: Cannot read properties of undefined (reading 'and') × 类型错误:无法读取未定义的属性(读取“名称”) - × TypeError: Cannot read properties of undefined (reading 'name') TypeError:无法读取未定义的属性(读取“名称”); - TypeError: Cannot read properties of undefined (reading 'name'); TypeError:无法读取未定义的属性(读取“v”) - TypeError: Cannot read properties of undefined (reading 'v')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM