码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • react 使用useRoutes遇到的问题


    问题一:Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.

      这个是在写路由表的时候,element尝试使用了()=>{import "xxx"}方法,报的错。在这里,element只能是一个React.ReactNode。

    我们仔细阅读源码,就能知道useRoutes接收的第一个参数是一个 数组 (RouteObject[]),而数组中的RouteObject 必须包含以下参数:

    问题二:React Hook "useRoutes" cannot be called at the top level. React Hooks must be called in a React function component or a custom React Hook function.eslintreact-hooks/rules-of-hooks.    

    这个错的意思是React Hook "useRoutes" 不能在顶层调用。 React Hooks 必须在 React 函数组件或自定义 React Hook 函数中调用。

    错误写法:

    1. import { useRoutes, Navigate } from "react-router-dom";
    2. import Main from "../pages/main";
    3. import Home from "../pages/home/home";
    4. import Gh from "../pages/gh/gh";
    5. const routes = useRoutes([
    6. {
    7. path: "/",
    8. element: <Navigate to="/main/home" />,
    9. },
    10. {
    11. path: "/main",
    12. element: <Main />,
    13. children: [
    14. {
    15. path: "gh",
    16. element: <Gh />,
    17. },
    18. {
    19. path: "home",
    20. element: <Home />,
    21. },
    22. ],
    23. },
    24. ]);
    25. export default routes;

    在这里 直接使用了useRoutes,外层也没有任何函数包裹。useRoutes是React Hook那么就要遵守Hook的调用规则。那么我们就来看看React Hook的使用规则。Hook 规则 – Reacthttps://zh-hans.reactjs.org/docs/hooks-rules.html

     我们可以看到,只能在react函数中调用。既然找到了错误,那么正确的写法就来啦

    1. import { useRoutes, Navigate } from "react-router-dom";
    2. import Main from "../pages/main";
    3. import Home from "../pages/home/home";
    4. import Gh from "../pages/gh/gh";
    5. const Routes = () => {
    6. const routes = useRoutes([
    7. {
    8. path: "/",
    9. element: <Navigate to="/main/home" />,
    10. },
    11. {
    12. path: "/main",
    13. element: <Main />,
    14. children: [
    15. {
    16. path: "gh",
    17. element: <Gh />,
    18. },
    19. {
    20. path: "home",
    21. element: <Home />,
    22. },
    23. ],
    24. },
    25. ]);
    26. return routes;
    27. };
    28. export default Routes;

    总结:在使用中我们要多发现问题,找到根本原因,多读官方文档,有助于更好的理解哦~

  • 相关阅读:
    Java读写文件时的GBK和UTF8转换问题
    解析几何:计算两条线段的交点
    Spring框架概述及核心设计思想
    ubuntu服务器中运行flask
    软件工程:波斯特尔定律,输入输出的平衡之道
    Python Day9 字符串进阶【零基础】
    【tg】9 : InstanceImpl 、 虚拟的音频渲染设备FakeAudioDeviceModuleImpl
    EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
    Mysql之存储引擎
    linux下的版本控制器git
  • 原文地址:https://blog.csdn.net/Lora_0925/article/details/125558179
  • 最新文章
  • 【FHE】我们如何实现同态加密推理(八):SiLU 的密文化——两条路径,和一个 8 字节的开关
    SQLcl 中 c/old/new 替换变量为何失效?先定位再替换
    不让模型写作文,直接从它脑子里读答案:Jev 决策在 .NET 的两条路线
    Halo插件:支持GitHub的图床附件策略
    电视投屏界的“一股清流”!SimpleTVCast 6.0.0-beta 抢先体验:无需TV端App,界面大换血!✨
    华三VXLAN集中式网关实战指南:Spine多活网关 + 双活接入(M-LAG)
    软件更好写之后口碑会更重要
    大 O、大 Omega、大 Theta 的区别,O(n) 复杂度是什么意思
    Java标签跳转:一个鲜为人知但却很实用的小技巧
    多智能体系统论文速读
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号