reactjs Chakra UI即使在正确安装后也无法工作

mwecs4sa  于 2023-10-17  发布在  React
关注(0)|答案(2)|浏览(130)

我已经遵循了React.js中关于Chakra UI安装的文档。我正在处理的组件在一个单独的项目中工作,但当我将其添加到当前项目时,它并不具有样式。而是显示纯html
index.js

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import App from "./App";
  4. import { ChakraProvider, theme } from "@chakra-ui/react";
  5. import Test from "./pages/Test";
  6. const root = ReactDOM.createRoot(document.getElementById("root"));
  7. root.render(
  8. <React.StrictMode>
  9. <ChakraProvider theme={theme} />
  10. <Test />
  11. <ChakraProvider />
  12. </React.StrictMode>
  13. );

Test.js

  1. import React, { ReactNode } from 'react';
  2. import {
  3. IconButton,
  4. Avatar,
  5. Box,
  6. CloseButton,
  7. Flex,
  8. HStack,
  9. VStack,
  10. Icon,
  11. useColorModeValue,
  12. Link,
  13. Drawer,
  14. DrawerContent,
  15. Text,
  16. useDisclosure,
  17. BoxProps,
  18. FlexProps,
  19. Menu,
  20. MenuButton,
  21. MenuDivider,
  22. MenuItem,
  23. MenuList,
  24. } from '@chakra-ui/react';
  25. import {
  26. FiHome,
  27. FiTrendingUp,
  28. FiCompass,
  29. FiStar,
  30. FiSettings,
  31. FiMenu,
  32. FiBell,
  33. FiChevronDown,
  34. } from 'react-icons/fi';
  35. import { IconType } from 'react-icons';
  36. import { ReactText } from 'react';
  37. import Rideshare from './Rideshare'
  38. ....
  39. rest of my code

package.json

  1. .....
  2. "dependencies": {
  3. "@chakra-ui/react": "^2.3.7",
  4. "@emotion/react": "^11.10.5",
  5. "@emotion/styled": "^11.10.5",
  6. "@react-google-maps/api": "^2.13.1",
  7. "@testing-library/jest-dom": "^5.16.4",
  8. "@testing-library/react": "^13.3.0",
  9. "@testing-library/user-event": "^13.5.0",
  10. "axios": "^0.19.2",
  11. "bootstrap": "^5.1.3",
  12. "dayjs": "^1.11.6",
  13. "framer-motion": "^6.5.1",
  14. "http-proxy-middleware": "^2.0.6",
  15. "jwt-decode": "^3.1.2",
  16. "query-string": "^6.13.7",
  17. "react": "^18.2.0",
  18. "react-dom": "^18.2.0",
  19. "react-icons": "^4.6.0",
  20. "react-redux": "^7.2.0",
  21. "react-router-dom": "^6.3.0",
  22. "react-scripts": "5.0.1",
  23. "react-slick": "^0.29.0",
  24. "reactstrap": "^9.1.1",
  25. "redux": "^4.0.5",
  26. "redux-devtools-extension": "^2.13.8",
  27. "redux-thunk": "^2.3.0",
  28. "remixicon": "^2.5.0",
  29. "slick-carousel": "^1.8.1",
  30. "web-vitals": "^2.1.4"
  31. },
  32. .....

我尝试使用命令重新安装Chakra-UI

  1. npm i @chakra-ui/react @emotion/react @emotion/styled framer-motion

但这个解决方案并不奏效我的package.json显示Chakra-UI已安装。

xkftehaa

xkftehaa1#

我已经去了,并试图检查了一些documentation在这方面。看起来你需要把脉轮提供者放在你项目的根目录下。
另一个问题可能是<React.StrictMode>不呈现任何可见的UI,因此您可能必须删除它

yks3o0rb

yks3o0rb2#

在index.js文件中有一个错字。
将index.js更正为以下内容,

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import App from "./App";
  4. import { ChakraProvider, theme } from "@chakra-ui/react";
  5. import Test from "./pages/Test";
  6. const root = ReactDOM.createRoot(document.getElementById("root"));
  7. root.render(
  8. <React.StrictMode>
  9. <ChakraProvider theme={theme}>
  10. <Test />
  11. </ChakraProvider>
  12. </React.StrictMode>
  13. );

希望这有帮助;)

展开查看全部

相关问题