使用React+ Shopify应用程序节点投掷错误

发布于 2025-02-08 20:37:05 字数 2758 浏览 4 评论 0原文

我正在使用React + Node在Shopify中创建一个应用程序,但它的投掷错误是:

未接收错误:元素类型无效:预期字符串(用于内置组件)或类/函数(用于复合组件),但得到:目的。

我不知道为什么会发生这种情况。任何建议都将不胜感激。提前致谢。

homepage.jsx

import {Page} from "@shopify/polaris";

import React, {Component} from 'react';
import { ResourcePicker } from "@shopify/app-bridge/actions";

class HomePage extends Component {

constructor(props) {
super(props);

this.state = {
  open : false,
}
}

render () {
return (
  
  <Page 
    fullWidth
    title='Product Selector' 
    primaryAction={{
      content: 'Select Products',
      onAction: () => this.setState({open: true})
    }}>
      
      <ResourcePicker
        resourceType = 'Product'
        open={true}
      />
      
  </Page>
  
)
}
}

export default HomePage;

app.jsx

import {
  ApolloClient,
  ApolloProvider,
  HttpLink,
  InMemoryCache,
} from "@apollo/client";
import {
  Provider as AppBridgeProvider,
  useAppBridge,
} from "@shopify/app-bridge-react";
import { authenticatedFetch } from "@shopify/app-bridge-utils";
import { Redirect } from "@shopify/app-bridge/actions";
import { AppProvider as PolarisProvider } from "@shopify/polaris";
import translations from "@shopify/polaris/locales/en.json";
import "@shopify/polaris/build/esm/styles.css";

import HomePage from "./components/HomePage";

export default function App() {
  return (
    <PolarisProvider i18n={translations}>
      <AppBridgeProvider
        config={{
          apiKey: process.env.SHOPIFY_API_KEY,
          host: new URL(location).searchParams.get("host"),
          forceRedirect: true,
        }}
      >
        <MyProvider>
          <HomePage />
        </MyProvider>
      </AppBridgeProvider>
    </PolarisProvider>
  );
}

function MyProvider({ children }) {
  const app = useAppBridge();

  const client = new ApolloClient({
    cache: new InMemoryCache(),
    link: new HttpLink({
      credentials: "include",
      fetch: userLoggedInFetch(app),
    }),
  });

  return <ApolloProvider client={client}>{children}</ApolloProvider>;
}

export function userLoggedInFetch(app) {
  const fetchFunction = authenticatedFetch(app);

  return async (uri, options) => {
    const response = await fetchFunction(uri, options);

    if (
      response.headers.get("X-Shopify-API-Request-Failure-Reauthorize") === "1"
    ) {
      const authUrlHeader = response.headers.get(
        "X-Shopify-API-Request-Failure-Reauthorize-Url"
      );

      const redirect = Redirect.create(app);
      redirect.dispatch(Redirect.Action.APP, authUrlHeader || `/auth`);
      return null;
    }

    return response;
  };
}

i'm creating a app in Shopify using React + Node but it is throwing error as :

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

I don't know why this is happening. Any suggestions would be much appreciated. Thanks in advance.

HomePage.jsx

import {Page} from "@shopify/polaris";

import React, {Component} from 'react';
import { ResourcePicker } from "@shopify/app-bridge/actions";

class HomePage extends Component {

constructor(props) {
super(props);

this.state = {
  open : false,
}
}

render () {
return (
  
  <Page 
    fullWidth
    title='Product Selector' 
    primaryAction={{
      content: 'Select Products',
      onAction: () => this.setState({open: true})
    }}>
      
      <ResourcePicker
        resourceType = 'Product'
        open={true}
      />
      
  </Page>
  
)
}
}

export default HomePage;

App.jsx

import {
  ApolloClient,
  ApolloProvider,
  HttpLink,
  InMemoryCache,
} from "@apollo/client";
import {
  Provider as AppBridgeProvider,
  useAppBridge,
} from "@shopify/app-bridge-react";
import { authenticatedFetch } from "@shopify/app-bridge-utils";
import { Redirect } from "@shopify/app-bridge/actions";
import { AppProvider as PolarisProvider } from "@shopify/polaris";
import translations from "@shopify/polaris/locales/en.json";
import "@shopify/polaris/build/esm/styles.css";

import HomePage from "./components/HomePage";

export default function App() {
  return (
    <PolarisProvider i18n={translations}>
      <AppBridgeProvider
        config={{
          apiKey: process.env.SHOPIFY_API_KEY,
          host: new URL(location).searchParams.get("host"),
          forceRedirect: true,
        }}
      >
        <MyProvider>
          <HomePage />
        </MyProvider>
      </AppBridgeProvider>
    </PolarisProvider>
  );
}

function MyProvider({ children }) {
  const app = useAppBridge();

  const client = new ApolloClient({
    cache: new InMemoryCache(),
    link: new HttpLink({
      credentials: "include",
      fetch: userLoggedInFetch(app),
    }),
  });

  return <ApolloProvider client={client}>{children}</ApolloProvider>;
}

export function userLoggedInFetch(app) {
  const fetchFunction = authenticatedFetch(app);

  return async (uri, options) => {
    const response = await fetchFunction(uri, options);

    if (
      response.headers.get("X-Shopify-API-Request-Failure-Reauthorize") === "1"
    ) {
      const authUrlHeader = response.headers.get(
        "X-Shopify-API-Request-Failure-Reauthorize-Url"
      );

      const redirect = Redirect.create(app);
      redirect.dispatch(Redirect.Action.APP, authUrlHeader || `/auth`);
      return null;
    }

    return response;
  };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最偏执的依靠 2025-02-15 20:37:05

我认为,

host: new URL(location).searchParams.get("host"),

如果找不到host找不到host

我建议将host添加到env变量并将其像使用一样

host: process.env.host

I think it's this line

host: new URL(location).searchParams.get("host"),

which might return null if host not found

I recommend adding the host to env variables and use it like

host: process.env.host
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文