Console error:[上述错误发生在<Link>组件中:]

huangapple go评论84阅读模式
英文:

Console error:[The above error occurred in the <Link> component:]

问题

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <App />
);
英文:

enter image description here

---------error on console:-------------------

> index.js:1 The above error occurred in the <Link> component:
>
> at LinkWithRef (http://localhost:1234/index.3d214d75.js:27970:11)
> at div
> at Header
> at App
>
> Consider adding an error boundary to your tree to customize error
> handling behavior. Visit https://reactjs.org/link/error-boundaries to
> learn more about error boundaries.

enter image description here

So I am trying to load cart component of my App on click of the image of the cart which is on my Header component using react-router-dom but getting the following error on console.

index.js

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom/client&#39;;
import &#39;./index.css&#39;
import App from &#39;./App&#39;;
 
const root = ReactDOM.createRoot(document.getElementById(&#39;root&#39;));
root.render(
  &lt;App /&gt;
);

App.js

import React from &quot;react&quot;;
import Header from &quot;./Components/Header&quot;;
import Body from &quot;./Components/Body&quot;;
import Footer from &quot;./Components/Footer&quot;;
import { createBrowserRouter, Outlet, RouterProvider } from &quot;react-router-dom&quot;;
import RestaurantMenu from &quot;./Components/RestaurantMenu&quot;;
import Error from &quot;./Components/Error&quot;;
import Cart from &quot;./Components/Cart&quot;;
import store from &quot;./Store/store&quot;;
import { Provider } from &quot;react-redux&quot;;

export const router = createBrowserRouter([
  {
    path: &quot;/&quot;,
    element: &lt;Body /&gt;,
    errorElement: &lt;Error /&gt;,
  },
  {
    path: &quot;/restaurant/:id&quot;,
    element: &lt;RestaurantMenu /&gt;,
    errorElement: &lt;Error /&gt;,
  },
  {
    path: &quot;/cart&quot;,
    element: &lt;Cart /&gt;,
    errorElement: &lt;Error /&gt;,
  },
]);

function App() {
  return (
    &lt;&gt;
      &lt;RouterProvider router={router} fallbackElement={&lt;Error /&gt;} /&gt;
      &lt;Header /&gt;
      &lt;Provider store={store}&gt;
        &lt;Outlet /&gt;
        &lt;Footer /&gt;
      &lt;/Provider&gt;
    &lt;/&gt;
  );
}

export default App;

Header.js

import React from &quot;react&quot;;
import { Link } from &quot;react-router-dom&quot;;

const Header = () =&gt; {
  return (
    &lt;div className=&quot;flex w-[100%] items-center justify-between shadow-lg h-14 absolute left-0 top-0&quot;&gt;
      &lt;div className=&quot;flex items-center &quot;&gt;
        &lt;div&gt;
          &lt;img
            src=&quot;https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQjunLTj0CdbpCMM0yRd61KuvwQyvVf8O-DKg&amp;usqp=CAU&quot;
            alt=&quot;logo&quot;
            className=&quot;h-16 p-2 mx-2&quot;
          /&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;p className=&quot;font-bold text-xl&quot;&gt;FOODELIVERY&lt;/p&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div&gt;
        &lt;ul className=&quot;flex&quot;&gt;
          &lt;li className=&quot;text-xl px-4 cursor-pointer&quot;&gt;Home&lt;/li&gt;
          &lt;li className=&quot;text-xl px-4 cursor-pointer&quot;&gt;Contact&lt;/li&gt;
          &lt;li className=&quot;text-xl px-4 cursor-pointer&quot;&gt;About&lt;/li&gt;
          &lt;li className=&quot;text-xl px-4 cursor-pointer&quot;&gt;Instamart&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
      &lt;Link to=&quot;/cart&quot;&gt;
        &lt;div className=&quot;flex items-center&quot;&gt;
          &lt;div&gt;
            &lt;img
              className=&quot;h-16 p-2&quot;
              src=&quot;data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/Ex/2Q==&quot;
              alt=&quot;cart&quot;
            /&gt;
          &lt;/div&gt;
          &lt;div className=&quot;mr-4&quot;&gt;
            &lt;span className=&quot;text-2xl text-red-600 font-bold&quot;&gt;(0)&lt;/span&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/Link&gt;
    &lt;/div&gt;
  );
};
 
export default Header;

Cart.js

import React from &#39;react&#39;

const Cart = () =&gt; {
  return (
    &lt;div className=&#39;mt-14&#39;&gt;Cart&lt;/div&gt;
  )
}

export default Cart

答案1

得分: 0

你没有分享实际问题或错误是什么,但根据代码,我会说这与在路由上下文之外呈现LinkOutlet组件有关。

创建一个布局路由组件来呈现HeaderOutlet,以便所有内容都呈现在RouterProvider提供的路由上下文中。

示例:

import React from "react";
import Header from "./Components/Header";
import Body from "./Components/Body";
import Footer from "./Components/Footer";
import { createBrowserRouter, Outlet, RouterProvider } from "react-router-dom";
import RestaurantMenu from "./Components/RestaurantMenu";
import Error from "./Components/Error";
import Cart from "./Components/Cart";
import store from "./Store/store";
import { Provider } from "react-redux";

const AppLayout = () => (
  <>
    <Header />
    <Outlet />
    <Footer />
  </>
);

export const router = createBrowserRouter([
  {
    element: <AppLayout />,
    children: [
      {
        path: "/",
        element: <Body />,
        errorElement: <Error />,
      },
      {
        path: "/restaurant/:id",
        element: <RestaurantMenu />,
        errorElement: <Error />,
      },
      {
        path: "/cart",
        element: <Cart />,
        errorElement: <Error />,
      },
    ]
  },
]);

function App() {
  return (
    <Provider store={store}>
      <RouterProvider router={router} fallbackElement={<Error />} />
    </Provider>
  );
}

export default App;
英文:

You didn't share what the actual issue or error is/was, but based on the code I'll say it's something to do with rendering Link and Outlet components outside a routing context.

Create a layout route component to render the Header and Outlet so that all content is rendered into the routing context the RouterProvider provides.

Example:

import React from &quot;react&quot;;
import Header from &quot;./Components/Header&quot;;
import Body from &quot;./Components/Body&quot;;
import Footer from &quot;./Components/Footer&quot;;
import { createBrowserRouter, Outlet, RouterProvider } from &quot;react-router-dom&quot;;
import RestaurantMenu from &quot;./Components/RestaurantMenu&quot;;
import Error from &quot;./Components/Error&quot;;
import Cart from &quot;./Components/Cart&quot;;
import store from &quot;./Store/store&quot;;
import { Provider } from &quot;react-redux&quot;;

const AppLayout = () =&gt; (
  &lt;&gt;
    &lt;Header /&gt;
    &lt;Outlet /&gt;
    &lt;Footer /&gt;
  &lt;/&gt;
);

export const router = createBrowserRouter([
  {
    element: &lt;AppLayout /&gt;,
    children: [
      {
        path: &quot;/&quot;,
        element: &lt;Body /&gt;,
        errorElement: &lt;Error /&gt;,
      },
      {
        path: &quot;/restaurant/:id&quot;,
        element: &lt;RestaurantMenu /&gt;,
        errorElement: &lt;Error /&gt;,
      },
      {
        path: &quot;/cart&quot;,
        element: &lt;Cart /&gt;,
        errorElement: &lt;Error /&gt;,
      },
    ]
  },
]);

function App() {
  return (
    &lt;Provider store={store}&gt;
      &lt;RouterProvider router={router} fallbackElement={&lt;Error /&gt;} /&gt;
    &lt;/Provider&gt;
  );
}

export default App;

huangapple
  • 本文由 发表于 2023年5月10日 23:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定