reactjs - 如何在嵌入式 React App 中的页面之间路由?

背景:

我正在尝试在我的嵌入式 Shopify 应用中创建一些链接。

我知道我不能使用简单的 <a>标记,因为 Shopify 嵌入式应用程序呈现为 iframe。

我在本教程中取得了一些进展,但我被卡住了:https://theunlikelydeveloper.com/shopify-app-bridge-react-router/

我正在尝试做的事情:

我有 3 个页面(index.js、dashboard.js 和 support.js)。我想允许用户从一个页面导航到另一个页面(使用链接和/或按钮)。

我的代码:

按照上面的教程,我已经走到这一步了:

// index.js
import { Page, Frame } from "@shopify/polaris";

const Index = () => {
  return (
    <Page>
      <Frame>
        {/* LINK TO DASHBOARD PAGE*/}
        {/* LINK TO SUPPORT PAGE */}
      </Frame>
    </Page>
  );
};

export default Index;
// routes.js
import React from "react";
import { Switch, Route, withRouter } from "react-router";
import { ClientRouter, RoutePropagator } from "@shopify/app-bridge-react";

function Routes(props) {
  const { history, location } = props;

  return (
    <>
      <ClientRouter history={history} />
      <RoutePropagator location={location} />
      <Switch>
        <Route path="/dashboard">
          <Dashboard />
        </Route>
        <Route path="/support">
          <Support />
        </Route>
        <Route path="/">
          <Home />
        </Route>
      </Switch>
    </>
  );
}

export default withRouter(Routes);
// link.js
import React from "react";
import { Link as ReactRouterLink } from "react-router";

const IS_EXTERNAL_LINK_REGEX = /^(?:[a-z][a-z\d+.-]*:|\/\/)/;

function Link({ children, url = "", external, ref, ...rest }) {
  if (external || IS_EXTERNAL_LINK_REGEX.test(url)) {
    rest.target = "_blank";
    rest.rel = "noopener noreferrer";
    return (
      <a href={url} {...rest}>
        {children}
      </a>
    );
  }

  return (
    <ReactRouterLink to={url} {...rest}>
      {children}
    </ReactRouterLink>
  );
}

export default Link;

附加:

我相信我应该在某处实现以下代码,但我看不出它如何适合使用链接或按钮在页面之间导航的画面。

<AppProvider linkComponent={Link}>
  {/* App content including your <Route> components */}
</AppProvider>
  • Shopify 文档链接:https://polaris.shopify.com/components/structure/app-provider#section-using-linkcomponent

最佳答案

在构建嵌入式应用程序时,您可以使用应用程序桥实用程序进行客户端导航,引用 in this answer

您只需要编辑 _app 文件并考虑从您的组件进行客户端导航(不能使用普通链接)


import {useEffect} from 'react';
import Router, { useRouter } from "next/router";
import { RoutePropagator as ShopifyRoutePropagator } from "@shopify/app-bridge-react";


function RoutePropagator () {
  const router = useRouter();
  const { route } = router;
  const app= useAppBridge();

  useEffect(() => {
    app.subscribe(Redirect.Action.APP, ({ path }) => {
      Router.push(path);
    });
  }, []);

  return app && route ? (
    <ShopifyRoutePropagator location={route} />
    ) : null;
}

然后在你的_app文件中使用这个组件

_app.tsx


class MyApp extends App {

  render() {

    const { Component, pageProps, host } = this.props as any;

    return (
      <PolarisProvider i18n={translations}>

        <ShopifyBridgeProvider
          config={{
            apiKey: API_KEY,
              host,
              forceRedirect: true,
          }}
        >
          <RoutePropagator />
          <ApolloClientProvider Component={Component} {...pageProps} />

        </ShopifyBridgeProvider>

      </PolarisProvider>
    );
  }
}

现在您已经在_app 文件中订阅了路由事件,我们只需要在您的页面中进行客户端导航

import {useAppBridge} from '@shopify/app-bridge-react';
import  { Redirect } from '@shopify/app-bridge/actions';

function IndexPage(props) {

  const app = useAppBridge();

  return (
    <>
      <div>{'you are in main page'}</div>

      <div onClick={() => {
        app.dispatch(Redirect.toApp({
          path: '/dashboard'
        }));
      }}>
        {'to dashboard'}
      </div>
    </>
  );

}

为了返回主页 / 路由,我发现如果没有提供商店名称,它会再次触发 oauth,所以我们将使用商店查询参数

 <div onClick={() => {
      app.dispatch(Redirect.toApp({
        path: '/?shop=<shop-name>.myshopify.com'
      }));
    }}>
      {'to main'}
 </div>

https://stackoverflow.com/questions/66996055/

相关文章:

java - 未找到 native micronaut 数据插入查询的可能实现

javascript - Css 过渡不适用于 React 中的条件渲染

telegram-bot - 用于从机器人类发送的消息的 CallbackQueryHandler

python - 如何使 Prophet 的输出静音?

azure - 对于高级、低延迟、带有搜索功能的大量小型 json 文件,Azure BlockBl

c# - 尽管有效的正则表达式和激活的解析在 gitlab 中没有覆盖率报告

google-apps-script - 在多个工作表中搜索一个词

python - datetime.combine with timezone 不同于 dateti

azure - Get-AzRoleAssignment 在 Azure Runbook 中引发 M

angular - 如何在 Angular 中将 patchValue 与 FormArray 一起