简体   繁体   English

它的实例类型“BrowserRouter”不是有效的 JSX 元素

[英]Its instance type 'BrowserRouter' is not a valid JSX element

I work on a project developed in React + coreui that has always worked perfectly.我从事一个使用 React + coreui 开发的项目,该项目一直运行良好。 However, in the last week, I had an inexplicable problem when deploying a modification.但是,在上周,我在部署修改时遇到了一个莫名其妙的问题。

18:03:11  Creating an optimized production build...
18:04:32  Failed to compile.
18:04:32  
18:04:32  /var/lib/jenkins/workspace/deploy-sandbox/src/AppRouter.tsx
18:04:32  Type error in /var/lib/jenkins/workspace/deploy-sandbox/src/AppRouter.tsx(47,6):
18:04:32  'Router' cannot be used as a JSX component.
18:04:32    Its instance type 'BrowserRouter' is not a valid JSX element.
18:04:32      The types returned by 'render()' are incompatible between these types.
18:04:32        Type 'React.ReactNode' is not assignable to type 'import("/var/lib/jenkins/workspace/deploy-sandbox/node_modules/@types/react-transition-group/node_modules/@types/react/index").ReactNode'.  TS2786
18:04:32  
18:04:32      45 | 
18:04:32      46 |   return (
18:04:32    > 47 |     <Router>
18:04:32         |      ^

I haven't modified anything in the project's structure or dependencies that could generate this error.我没有修改项目结构或依赖项中可能产生此错误的任何内容。

I believe there is some dependency conflict because from that moment on, it happens constantly.我相信存在一些依赖冲突,因为从那一刻起,它一直在发生。

I've tried many alternatives to try to solve the problem, but so far, without success.我尝试了许多替代方法来尝试解决问题,但到目前为止,都没有成功。 Has anyone experienced this and know of a solution?有没有人经历过这种情况并知道解决方案? Or Do you know where to find the problem?或者你知道哪里可以找到问题吗?


  "dependencies": {
    "@coreui/chartjs": "^3.0.0",
    "@coreui/coreui-pro": "^3.4.2",
    "@coreui/coreui": "^4.1.3",
    "@coreui/icons-pro": "^2.0.3",
    "@coreui/icons-react": "^1.1.0",
    "@coreui/icons": "^2.1.0",
    "@coreui/react-chartjs": "^1.1.0",
    "@coreui/react": "^3.4.6",
    "@coreui/utils": "^1.3.1",
    "del": "^6.0.0",
    "gulp-zip": "^5.1.0",
    "gulp": "^4.0.2",
    "http-status": "^1.5.0",
    "moment": "^2.29.2",
    "react-dom": "^17.0.2",
    "react-hook-form": "^7.29.0",
    "react-imask": "^6.4.2",
    "react-papaparse": "^3.18.2",
    "react-password-strength-bar": "^0.4.0",
    "react-query": "^3.34.19",
    "react-router-dom": "^5.3.0",
    "react-router": "^5.2.1",
    "react-scripts": "^5.0.0",
    "react-select": "^5.2.2",
    "react": "^17.0.2",
    "spinkit": "^2.0.1",
    "use-debounce": "^7.0.1",
    "web-vitals": "^2.1.4"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ],
    "sass": "^1.32.8"
  },
  "eslint-config-prettier": "=8.3.0",
  "devDependencies": {
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.4.0",
    "@types/node": "^17.0.7",
    "@types/react": "^17.0.38",
    "@types/react-dom": "^17.0.11",
    "@types/react-router-dom": "^5.3.2",
    "@types/react-select": "^5.0.1",
    "@typescript-eslint/eslint-plugin": "^5.8.1",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-import-resolver-alias": "^1.1.2",
    "eslint-plugin-import": "^2.25.4",
    "eslint-plugin-react": "=7.28.0",
    "http-proxy-middleware": "^2.0.1",
    "sass": "^1.45.2",
    "typescript": "^4.5.4"
  }

There is another similar question where the user updates the version of react and react-dom.还有另一个类似的问题是用户更新 react 和 react-dom 的版本。

Some components "cannot be used as a JSX component" 某些组件“不能用作 JSX 组件”

I can't implement this solution as I have dependencies that are incompatible with newer versions.我无法实现此解决方案,因为我的依赖项与较新版本不兼容。

I FOUND THE SOLUTION!!!我找到了解决方案!!!

the packages包裹

       "@types/react": "^17.0.38",
       "@types/react-dom": "^17.0.11",

were imported into devDependencies.被导入到 devDependencies。

We transferred this import to dependencies, cleared the cache from the deploy server and everything went back to normal.我们将此导入转移到依赖项,从部署服务器清除缓存,一切恢复正常。

try adding "@types/react" in the "resolutions" section of your package.json尝试在 package.json 的“分辨率”部分添加“@types/react”

this fixed my problem cause the dependencies of "@types/react-dom" on my package.lock.json is referencing "@types/react" version:"*".这解决了我的问题,导致“@types/react-dom”对我的 package.lock.json 的依赖关系引用“@types/react”版本:“*”。

"resolutions": {
    "@types/react": "17.0.38"
},

Hope this helps.希望这可以帮助。

暂无
暂无

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

相关问题 &#39;Router&#39; 不能用作 JSX 组件。 它的实例类型“BrowserRouter”不是有效的 JSX 元素 - 'Router' cannot be used as a JSX component. Its instance type 'BrowserRouter' is not a valid JSX element 它的返回类型 'void' 不是有效的 JSX 元素 - Its return type 'void' is not a valid JSX element 它的实例类型'Provider<anyaction> ' 不是有效的 JSX 元素。 render() 返回的类型</anyaction> - Its instance type 'Provider<AnyAction>' is not a valid JSX element. The types returned by render() 'Provider' 不能用作 JSX 组件。 它的实例类型'Provider<anyaction> ' 不是有效的 JSX 元素。 TS2786</anyaction> - 'Provider' cannot be used as a JSX component. Its instance type 'Provider<AnyAction>' is not a valid JSX element. TS2786 RC-dock 库的 &#39;DockLayout&#39; 不能用作 JSX 组件。 它的实例类型“DockLayout”不是有效的 JSX 元素 - RC-dock library's 'DockLayout' cannot be used as a JSX component. Its instance type 'DockLayout' is not a valid JSX element 组件不能用作 JSX 组件。 它的返回类型'元素 | undefined' 不是有效的 JSX 元素 - Component cannot be used as a JSX component. Its return type 'Element | undefined' is not a valid JSX element 组件不能用作 JSX 组件。 它的返回类型 'Element[]' 不是有效的 JSX 元素 - Component cannot be used as a JSX component. Its return type 'Element[]' is not a valid JSX element “X”不能用作 JSX 组件。 它的返回类型 'Element[]' 不是有效的 JSX 元素 - 'X' cannot be used as a JSX component. Its return type 'Element[]' is not a valid JSX element 它的返回类型 &#39;void | Element&#39; 不是有效的 JSX 元素。 类型 &#39;void&#39; 不能分配给类型 &#39;Element | 空值&#39; - Its return type 'void | Element' is not a valid JSX element. Type 'void' is not assignable to type 'Element | null' 它的返回类型 'Element | undefined' 不是有效的 JSX 元素。 类型“undefined”不可分配给类型“Element | 无效的' - Its return type 'Element | undefined' is not a valid JSX element. Type 'undefined' is not assignable to type 'Element | null'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM