useShoppingCart – 类型错误:无法读取未定义的属性(读取 ‘dispatch’)

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

useShoppingCart - TypeError: Cannot read properties of undefined (reading 'dispatch')

问题

我正在使用Stripe API和use-shopping-cart来开发一个电商网站但我遇到了一个错误似乎无法解决如果我将页面包裹在CartProvider中那个页面可以正常使用购物车但我想在整个应用程序中全局使用它所以我将整个应用程序都包裹在了CartProvider中但现在我遇到了这个错误我已经阅读了use-shopping-cart的文档但找不到解决方案

--相关代码--

_app.tsx

import "../styles/globals.css";
import type { AppProps } from "next/app";
import { CartProvider, DebugCart } from "use-shopping-cart";
import * as config from "../config";
import { ReactNode } from "react";

const Cart = ({ children }: { children: ReactNode }) => (
  <CartProvider
    cartMode="checkout-session"
    stripe={process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY as string}
    currency={config.CURRENCY}
    shouldPersist={false}
  >
    <>{children}</>
  </CartProvider>
);

export default function App({ Component, pageProps }: AppProps) {
  return (
    <Cart>
      <Component {...pageProps} />
    </Cart>
  );
}

这段代码可以在单个页面中正常工作:

"use client";
import { NextPage } from "next";

import Cart from "./Cart";
import CartSummary from "./CartSummary";
import Products from "./Products";

const Checkout: NextPage = () => {
  return (
    <div>
      <Cart>
        <CartSummary />
        <Products />
      </Cart>
    </div>
  );
};

export default Checkout;

我卡住了,任何帮助将不胜感激。当我尝试在整个应用程序中使用useShoppingCart来添加、删除产品和结账时,我会遇到这个错误。当我只在单个页面中使用时,它可以正常工作,但我需要在整个应用程序中使用它。

英文:

I am using Stripe API and use-shopping-cart for a e-commerce site but I have run into this error that I can't seem to figure out, if I wrap a page with CartProvider that single page works with the shoppingcart but I am trying to use it globally throughout the application. So I wrapped the entire app with CartProvider but now I am getting this error. I have read through the use-shopping-cart docs but can't find a solution.

--relevant code--

_app.tsx

import &quot;../styles/globals.css&quot;;
import type { AppProps } from &quot;next/app&quot;;
import { CartProvider, DebugCart } from &quot;use-shopping-cart&quot;;
import * as config from &quot;../config&quot;;
import { ReactNode } from &quot;react&quot;;

const Cart = ({ children }: { children: ReactNode }) =&gt; (
  &lt;CartProvider
    cartMode=&quot;checkout-session&quot;
    stripe={process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY as string}
    currency={config.CURRENCY}
    shouldPersist={false}
  &gt;
    &lt;&gt;{children}&lt;/&gt;
  &lt;/CartProvider&gt;
);

export default function App({ Component, pageProps }: AppProps) {
  return (
    &lt;Cart&gt;
      &lt;Component {...pageProps} /&gt;
    &lt;/Cart&gt;
  );
}

this works but only for this single page

&quot;use client&quot;;
import { NextPage } from &quot;next&quot;;

import Cart from &quot;./Cart&quot;;
import CartSummary from &quot;./CartSummary&quot;;
import Products from &quot;./Products&quot;;

const Checkout: NextPage = () =&gt; {
  return (
    &lt;div&gt;
      &lt;Cart&gt;
        &lt;CartSummary /&gt;
        &lt;Products /&gt;
      &lt;/Cart&gt;
    &lt;/div&gt;
  );
};

export default Checkout;

I am stuck, any help would be appreciated. I get this error when I try and use useShoppingCart throughout the application for adding, removing products and checkout. when I only do this in a single page it works but I need to use this throughout the app.

答案1

得分: 1

添加<Provider>来解决这个问题。

我在Next 13项目中遇到了相同的错误,使用useShoppingCart时会出现"TypeError: Cannot read properties of undefined (reading 'dispatch')"。

我忘记在layout.jsx/js中包裹组件,一旦添加如下内容,错误就得以解决。

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

&lt;Providers&gt;
    &lt;div&gt;
        &lt;SiteHeader /&gt;
        &lt;SiteBlob /&gt;
    &lt;div&gt;
        {children}
    &lt;/div&gt;
        &lt;SiteFooter /&gt;
    &lt;/div&gt;
&lt;/Providers&gt;

<!-- 结束代码片段 -->

英文:

Add <Provider> to solve it.

I got the same error in Next 13 project. whlie using useShoppingCart. it would give "TypeError: Cannot read properties of undefined (reading 'dispatch').

I had fogotten to wrap arround components in the layout.jsx/js once i added it as follows the error got resolved.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;Providers&gt;
    &lt;div&gt;
        &lt;SiteHeader /&gt;
        &lt;SiteBlob /&gt;
    &lt;div&gt;
        {children}
    &lt;/div&gt;
        &lt;SiteFooter /&gt;
    &lt;/div&gt;
&lt;/Providers&gt;

<!-- end snippet -->

答案2

得分: 0

在下面的代码中,你需要首先创建一个提供者文件,将其标记为客户端组件,然后包装你的布局文件。

"use client";
import React, { ReactNode } from "react";
import { CartProvider } from "use-shopping-cart";
import * as config from "../../config";
import { loadStripe } from "@stripe/stripe-js";

const stripePromise = loadStripe(
  process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY!
);

const Cart = ({ children }: { children: ReactNode }) => (
  <CartProvider
    cartMode="checkout-session"
    stripe={stripePromise as unknown as string}
    currency={config.CURRENCY}
    shouldPersist={false}
  >
    <>{children}</>
  </CartProvider>
);

export default Cart;

布局文件如下:

import "../styles/globals.css";
import Footer from "./Footer";
import Header from "./Header";
import Cart from "./Provider/Cart";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html>
      {/* <head /> */}
      <body>
        <Cart>
          <Header />
          {children}
          <Footer />
        </Cart>
      </body>
    </html>
  );
}
英文:

Turns out that in order to use global variables in next 13 you need to first create your provider file mark it as a client component and then wrap your layout file.

&quot;use client&quot;;
import React, { ReactNode } from &quot;react&quot;;
import { CartProvider } from &quot;use-shopping-cart&quot;;
import * as config from &quot;../../config&quot;;
import { loadStripe } from &quot;@stripe/stripe-js&quot;;

const stripePromise = loadStripe(
  process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY!
);

const Cart = ({ children }: { children: ReactNode }) =&gt; (
  &lt;CartProvider
    cartMode=&quot;checkout-session&quot;
    stripe={stripePromise as unknown as string}
    currency={config.CURRENCY}
    shouldPersist={false}
  &gt;
    &lt;&gt;{children}&lt;/&gt;
  &lt;/CartProvider&gt;
);

export default Cart;

--layout--

import &quot;../styles/globals.css&quot;;
import Footer from &quot;./Footer&quot;;
import Header from &quot;./Header&quot;;
import Cart from &quot;./Provider/Cart&quot;;

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    &lt;html&gt;
      {/* &lt;head /&gt; */}
      &lt;body&gt;
        &lt;Cart&gt;
          &lt;Header /&gt;
          {children}
          &lt;Footer /&gt;
        &lt;/Cart&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  );
}

huangapple
  • 本文由 发表于 2023年2月19日 00:49:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494844.html
匿名

发表评论

匿名网友

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

确定