Getting started with App Bridge React
Once you've set up your app, you can jump right into using Actions with plain JavaScript. If you're using React in your project, App Bridge React provides another way to work with App Bridge actions. To get started, you will need to add the App Bridge Provider and any custom routing features that you may want to use.
The App Bridge Provider
provides the app context for all the components within it. It accepts a config
object and an optional router
prop. For more information about props, refer to Provider props.
Example code
Anchor link to section titled "Example code"This example demostrates setting up the app with a Provider
and using the Loading component.
Import the Provider
from @shopify/app-bridge-react
and pass a config
object into it. You can then use App Bridge React components and hooks within the Provider
.
Using a custom router
Anchor link to section titled "Using a custom router"You may want to use a custom client-side router, such as react-router
, to manage navigation within your app. Prior to version 2.0.25
you would use the client router, along with the route propagator to manage custom routing. In newer versions of App Bridge React, the Provider
accepts an optional router
prop and configures custom routing for you.
Example code
Anchor link to section titled "Example code"Passing in a router will allow you to bypass setting up the client router and the route propagator. If you are using React Router, ensure that the Provider
is a child of the router component.
Provider Props
Anchor link to section titled "Provider Props"Name | Type | Description | Required |
---|---|---|---|
config | AppConfig |
Your application configuration | Yes |
router | RouterConfig |
Custom router configuration | No |
Name | Type | Description | Required |
---|---|---|---|
apiKey | string |
API key from Shopify Partner Dashboard | Yes |
host | string |
The hostname for the current shop. Learn more | Yes |
forceRedirect | boolean |
Use to toggle redirecting to the Shopify admin when the app is not opened inside the Shopify admin. Defaults to true . |
No |
RouterConfig
Anchor link to section titled "RouterConfig"Name | Type | Description | Required |
---|---|---|---|
history | {replace: (path) => navigate(path, {replace: boolean})} |
An object to control the history of the browser | Yes |
location | Location |
An object with the location infomation of the current page | Yes |
Using App Bridge React with Polaris
Anchor link to section titled "Using App Bridge React with Polaris"App Bridge React is fully compatible with Polaris.
To use them together, wrap your app in both Polaris’s <AppProvider>
component and App Bridge React’s <Provider>
component.
Accessing the App Bridge context directly
Anchor link to section titled "Accessing the App Bridge context directly"App Bridge React provides access to the App Bridge client app
instance using the React Context API.
Some ways to access the app context include:
useAppBridge
(recommended)useContext
(for Apps using version 1.24.0 and below)Context.Consumer
(using render props)
useAppBridge
Anchor link to section titled "useAppBridge"The useAppBridge
hook is available in version 1.25.0 and above. You can use this hook to access the App Bridge client app
in a functional component.
If you're using a version of App Bridge below 1.25.0 where the useAppBridge
hook isn't available, use React’s useContext
hook directly.
App Context with RenderProps
Anchor link to section titled "App Context with RenderProps"Use Context.Consumer
to get access to the App Bridge client app
in render props.
ClientRouter
Anchor link to section titled "ClientRouter"By default, App Bridge applies URL changes from outside your app by updating the iframe URL. If your app uses client-side routing, such as React Router, then you need to override this behavior to avoid unnecessary full-page reloads. ClientRouter
prevents App Bridge from changing the iframe URL, and enables you provide a custom client-side router, for example react-router
, to handle navigation. Use ClientRouter with any routing system that provides a history.replace
method, which accepts a string for the updated path.
You can use ClientRouter
as a hook or a component.
useClientRouting
hook
Anchor link to section titled "useClientRouting hook"In App.jsx
, set up a custom history object and pass it into a Router
component.
In the Router
component, pass the history
prop into the useClientRouting
hook.
<ClientRouter />
component
Anchor link to section titled "Pass the history
prop into the ClientRouter
component.
RoutePropagator
Anchor link to section titled "RoutePropagator"When a user navigates inside an embedded app, the URL of the embedded app iframe changes. If the user reloads the page, then the navigation isn't reflected in the URL of the parent page. RoutePropagator
enables you synchronize a Shopify embedded app's URL with the parent page.
You can also use the App Bridge History API to keep the parent URL in sync manually.
You can use RoutePropagator
as a hook or a component.
useRoutePropagation
hook
Anchor link to section titled "useRoutePropagation hook"Import or create a location
object and pass it onto a Routes
component (that you will create in the next step) in App.jsx
.
Create a Routes
component. Import useRoutePropagation
from @shopify/app-bridge-react
. Call useRoutePropagation
with the location prop and configure the routes according to your custom routing solution.
<RoutePropagator />
Anchor link to section titled "Import or create a location
object and pass it onto a RoutePropagator
component.