• Provider
  • Using a custom router
  • Provider Props
  • Using App Bridge React with Polaris React
  • Accessing the App Bridge context directly
  • ClientRouter
  • RoutePropagator

Getting started with App Bridge React

If you're using React in your project, then you should use the App Bridge React library. App Bridge React provides hooks and components that let you use App Bridge in a standard and familiar way inside your React application.

To get started, you need to add the App Bridge Provider and any custom routing features that you want to use.

Provider

Image for: Provider
Anchor link to section titled "Provider"

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 demonstrates 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.

Note

In the following example, config is a valid App Bridge configuration object. Learn more about configuring App Bridge.

Using a custom router

Image for: 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

Image for: 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

AppConfig

Anchor link to section titled "AppConfig"
Name Type Description Required
apiKey string The client ID 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 in production. Defaults to false in dev environments. 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 React

Image for: Using App Bridge React with Polaris React
Anchor link to section titled "Using App Bridge React with Polaris React"

App Bridge React is fully compatible with Polaris React. To use them together, wrap your app in both Polaris React’s <AppProvider> component and App Bridge React’s <Provider> component.

Accessing the App Bridge context directly

Image for: 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.

useContext

Anchor link to section titled "useContext"

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

Image for: ClientRouter
Anchor link to section titled "ClientRouter"

Note

Although the client router and route propagator utilies remain available in the current version of App Bridge React, we recommend avoiding the manual process of setting up these features. Instead, you can use the optional routing prop in the Provider to accomplish the same behavior.

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.

Note

In the following example, config is a valid App Bridge configuration object. Learn more about configuring App Bridge.

In the Router component, pass the history prop into the useClientRouting hook.

<ClientRouter /> component

Anchor link to section titled " component"

Pass the history prop into the ClientRouter component.

RoutePropagator

Image for: RoutePropagator
Anchor link to section titled "RoutePropagator"

Note

Although the client router and route propagator utilies remain available in the current version of App Bridge React, we recommend avoiding the manual process of setting up these features. Instead, you can use the optional routing prop in the Provider to accomplish the same behavior.

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"
  1. Import useRoutePropagation from @shopify/app-bridge-react.
  2. Call useRoutePropagation with a location parameter.
  3. Configure the routes according to your custom routing solution.

Note

In the following example, config is a valid App Bridge configuration object. Learn more about configuring App Bridge.

<RoutePropagator />

Anchor link to section titled ""

Import or create a location object and pass it onto a RoutePropagator component.

Note

In the following example, config is a valid App Bridge configuration object. Learn more about configuring App Bridge.

On this page

  • Provider
  • Using a custom router
  • Provider Props
  • Using App Bridge React with Polaris React
  • Accessing the App Bridge context directly
  • ClientRouter
  • RoutePropagator