“我只在浏览器中收到一个错误,这是错误”:
意外的应用程序错误!元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:你可能忘记了从定义你的组件的文件中导出它,或者你可能混淆了默认的和命名的导入。检查Layout
的render方法。
createFiberFromTypeAndProps@http://localhost:3000/static/js/bundle.js:39715:21
createFiberFromElement@http://localhost:3000/static/js/bundle.js:39738:46
createChild@http://localhost:3000/static/js/bundle.js:26685:54
reconcileChildrenArray@http://localhost:3000/static/js/bundle.js:26982:40
reconcileChildFibers@http://localhost:3000/static/js/bundle.js:27396:20
reconcileChildren@http://localhost:3000/static/js/bundle.js:31573:32
updateHostComponent$1@http://localhost:3000/static/js/bundle.js:32287:24
beginWork@http://localhost:3000/static/js/bundle.js:33945:18
beginWork$1@http://localhost:3000/static/js/bundle.js:38731:18
performUnitOfWork@http://localhost:3000/static/js/bundle.js:37929:16
workLoopSync@http://localhost:3000/static/js/bundle.js:37843:26
renderRootSync@http://localhost:3000/static/js/bundle.js:37812:11
recoverFromConcurrentError@http://localhost:3000/static/js/bundle.js:37221:38
performConcurrentWorkOnRoot@http://localhost:3000/static/js/bundle.js:37122:26
workLoop@http://localhost:3000/static/js/bundle.js:48787:46
flushWork@http://localhost:3000/static/js/bundle.js:48761:18
performWorkUntilDeadline@http://localhost:3000/static/js/bundle.js:49045:25
💿 嘿开发商👋
当应用抛出错误时,您可以通过提供自己的ErrorBoundary属性来提供比这更好的用户体验
“我一直在尝试在chatGPT中找到解决方案,但在我的浏览器中仍然出现同样的错误,我希望这里的任何人都能帮助我,给予我一个解决这个问题的方案”
“这是布局组件代码”:
import React from "react";
import { Outlet } from "react-router-dom";
import Footer from "../Footer/Footer";
import Navbar from "../Navbar/Navbar";
const Layout = () => {
return (
<div className="app">
<Navbar />
<Outlet />
<Footer />
</div>
);
};
export default Layout;
这是我的App.js文件:
import React from "react";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import Home from "./pages/Home/Home";
import Product from "./pages/Product/Product";
import Products from "./pages/Products/Products";
import Layout from "./Components/Layout/Layout";
const router = createBrowserRouter([
{
path: "/",
element: <Layout />,
children: [
{
path: "/",
element: <Home />,
},
{
path: "/products/:id",
element: <Products />,
},
{
path: "/product/:id",
element: <Product />,
},
],
},
]);
function App() {
return <RouterProvider router={router} />;
}
export default App;
这是Navbar.js:
import React from 'react'
const Navbar = () => {
return (
<div>
</div>
)
}
export default Navbar
这里是footer.js:
import React from 'react'
function Footer() {
return (
<div>
</div>
)
}
export default Footer
1条答案
按热度按时间wz3gfoph1#
如果您使用的是**
react-router-dom
,则是因为您访问的路径不正确case 1
:您已定义路由,但未使用正确的路由- 404 page by react-router-domcase 2
:您仅定义了react-router-dom
,未使用路径如果您没有使用
react-router-dom
**,那么您在某个地方没有为函数或组件传递正确的类型,然后共享快照或repo以便我理解