If you select the Shopify template upon app creation in Gadget, @gadgetinc/react-shopify-app-bridge is already installed.
This is a companion package to the JavaScript client package generated for your Gadget app. You must first install the client for your app, and then install this package.
To install the client for your app, you must set up the Gadget NPM registry:
Register the Gadget npm registry
yarn config set @gadget-client:registry https://registry.gadget.dev/npm
npm config set @gadget-client:registry https://registry.gadget.dev/npm
and then install your app's API client:
Install required packages
yarn add @gadget-client/example-app
npm install @gadget-client/example-app
Once you have your client installed, you can install the React hooks library and the Shopify App bridge library with yarn or npm:
Once isAuthenticated is true, you will be able to make authenticated API requests using your Gadget API client and the @gadgetinc/react hooks.
The Provider and useGadget are already set up for you when you connect to Shopify using Gadget's frontends.
Custom router
@shopify/app-bridge-react allows you to specify a custom router configuration to manage client-side routing. Similarly, the Gadget provider will allow you to specify a custom router which will be forwarded to the App Bridge.
JavaScript
1import{
2AppType,
3ProviderasGadgetProvider,
4 useGadget,
5}from"@gadgetinc/react-shopify-app-bridge";
6import{NavMenu}from"@shopify/app-bridge-react";
7import{ useEffect }from"react";
8import{
9Link,
10Outlet,
11Route,
12RouterProvider,
13 createBrowserRouter,
14 createRoutesFromElements,
15 useLocation,
16 useNavigate,
17}from"react-router";
18import{ api }from"../api";
19importIndexfrom"../routes/index";
20
21functionError404(){
22// use navigation hooks to handle 404 pages for undefined routes
79// use the Outlet, which renders the correct page given the current route
80// and use the App Bridge NavMenu for Shopify embedded app nav
81return(
82<>
83<Outlet/>
84<NavMenu>
85<Linkto="/"rel="home">
86 Shop Information
87</Link>
88</NavMenu>
89</>
90);
91}
92
93functionUnauthenticatedApp(){
94return<>App must be viewed in the Shopify Admin</>;
95}
96
97exportdefaultApp;
Creating a new Shopify app in Gadget will set up custom routing for you using either react-router or Remix. Remix apps will use
file-based routing.
Breaking changes when upgrading to version 0.14.0
Because @gadgetinc/react-shopify-app-bridge version 0.14.0 supports App Bridge V4, this requires you to migrate to your app from App Bridge V3.
Otherwise, you will experience many breaking changes to your app, this also means you will have to update any hooks or components that aren't supported by V4. To upgrade your Gadget application to support App Bridge V4, check out our guide here.
Reference
Provider
A Provider component that wraps your Shopify embedded app to authenticate the API client and generate a session token.
Props
api: Client - The Gadget API client to use for the extension.
shopifyApiKey: string - The API key for the Shopify Partner app.
type: AppType - Either AppType.Embedded which is the default when setting up a Shopify connection in Gadget, or AppType.Standalone for non-embedded apps.
A React hook that determines when the OAuth request is underway and if the user is authenticated.
Returns
loading: boolean - A boolean that is true when the OAuth request is in progress.
canAuth: boolean - A boolean that is true if the current browser window is ready to authenticate.
isAuthenticated: boolean - A boolean that is true when the user is authenticated.
appBridge: ShopifyGlobal | null - An instance of the App Bridge object. Only available when the appType is Embedded.
isEmbedded: boolean - A boolean that is true when the app is running inside the iframe in the Shopify admin.
isRootFrameRequest: boolean - A boolean that is true if the app is being rendered outside of the Shopify admin flow. Only applies when appType is Embedded.